PDA

View Full Version : BOOT_COMPLETED



mhn143
یک شنبه 06 بهمن 1398, 12:41 عصر
سلام
من یه سرویس دارم که هنگام ریست شدن دستگاه باید اجرا بشه
اگر ورژن android دستگاه از 7 پایین باشه اجرا میشه ولی از 7 به بالا اجرا نمیشه

mehdi.safavie
سه شنبه 22 بهمن 1398, 08:37 صبح
درود;
از JobIntentService (https://developer.android.com/reference/android/support/v4/app/JobIntentService.html)استفاده کنین.

<receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<service android:name=".MyService"
android:permission="android.permission.BIND_JOB_SERVICE"/>


public class BootReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAct ion())) {
MyService.enqueueWork(context, new Intent());
}
}

}


public class MyService extends JobIntentService {

public static final int JOB_ID = 0x01;

public static void enqueueWork(Context context, Intent work) {
enqueueWork(context, MyService.class, JOB_ID, work);
}

@Override
protected void onHandleWork(@NonNull Intent intent) {
// your code
}

}