Fix crash on Android O

This commit is contained in:
topjohnwu 2017-07-01 17:55:43 +08:00
parent 1daf5a611c
commit 789fef34ba
4 changed files with 51 additions and 21 deletions

View File

@ -70,7 +70,7 @@
<receiver android:name=".receivers.ManagerUpdate" />
<service android:name=".services.BootupIntentService" />
<service android:name=".services.OnBootIntentService" />
<service
android:name=".services.UpdateCheckService"

View File

@ -3,14 +3,28 @@ package com.topjohnwu.magisk.receivers;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import com.topjohnwu.magisk.services.BootupIntentService;
import com.topjohnwu.magisk.services.OnBootIntentService;
import com.topjohnwu.magisk.utils.Utils;
public class BootReceiver extends BroadcastReceiver {
private void startIntentService(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(new Intent(context, OnBootIntentService.class));
} else {
context.startService(new Intent(context, OnBootIntentService.class));
}
}
@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, BootupIntentService.class));
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Utils.getMagiskManager(context).initSU();
// There is currently no need to start an IntentService onBoot
// startIntentService(context);
}
}
}

View File

@ -1,18 +0,0 @@
package com.topjohnwu.magisk.services;
import android.app.IntentService;
import android.content.Intent;
import com.topjohnwu.magisk.MagiskManager;
public class BootupIntentService extends IntentService {
public BootupIntentService() {
super("BootupIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
((MagiskManager) getApplication()).initSU();
}
}

View File

@ -0,0 +1,34 @@
package com.topjohnwu.magisk.services;
import android.app.IntentService;
import android.content.Intent;
import android.os.Build;
import android.support.v7.app.NotificationCompat;
import com.topjohnwu.magisk.R;
public class OnBootIntentService extends IntentService {
private static final int ONBOOT_NOTIFICATION_ID = 3;
public OnBootIntentService() {
super("OnBootIntentService");
}
@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_magisk)
.setContentTitle("onBoot")
.setContentText("Running onBoot operations...");
startForeground(ONBOOT_NOTIFICATION_ID, builder.build());
}
}
@Override
protected void onHandleIntent(Intent intent) {
// Currently nothing to do
}
}