【问题标题】:Service in API 26API 26 中的服务
【发布时间】:2018-10-19 11:09:03
【问题描述】:

代码

public class MainService extends JobIntentService {
WindowManager wm;
LayoutInflater li;
RelativeLayout ll;
View myview;
int NOTIFICATION_ID = 1;
@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();

    String CHANNEL_ID = "my_service";
    String CHANNEL_NAME = "My Background Service";
    if (Build.VERSION.SDK_INT >= 26) {
        // in APIs 26+ we should show a notifications
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                CHANNEL_NAME, NotificationManager.IMPORTANCE_NONE);
        ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setCategory(Notification.CATEGORY_SERVICE).setPriority(PRIORITY_MIN).build();

        startForeground(NOTIFICATION_ID, notification);
    }

    wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    ll = new RelativeLayout(this);
    ll.setBackgroundColor(Color.RED);
    LinearLayout.LayoutParams layoutParameteres = new LinearLayout.LayoutParams(
            50, 500);
    ll.setBackgroundColor(Color.argb(66,255,0,0));
    ll.setLayoutParams(layoutParameteres);

    canDrawOnScreen();

    final WindowManager.LayoutParams parameters = new WindowManager.LayoutParams(
            50, 500, WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);
    parameters.gravity = Gravity.RIGHT | Gravity.CENTER;
    parameters.x = 0;
    parameters.y = 0;

    wm.addView(ll, parameters);
    WindowManager.LayoutParams updatedParameters = parameters;

    ll.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           Intent intent = new Intent(MainService.this,UsersService.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startService(intent);

        }
    });

}

@Override
public void onDestroy() {
    super.onDestroy();
    stopSelf();
}

@Override
protected void onHandleWork(@NonNull Intent intent) {

}
private boolean canDrawOnScreen() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return canDrawOnScreenM();
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        return canDrawOverlaysUsingReflection(getApplicationContext());
    } else
        return true;
}

@RequiresApi(api = Build.VERSION_CODES.M)
private boolean canDrawOnScreenM() {
    return Settings.canDrawOverlays(getApplicationContext());
}


@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public static boolean canDrawOverlaysUsingReflection(Context context) {
    try {
        AppOpsManager manager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
        Class clazz = AppOpsManager.class;
        Method dispatchMethod = clazz.getMethod("checkOp", new Class[]{int.class, int.class, String.class});
        //AppOpsManager.OP_SYSTEM_ALERT_WINDOW = 24
        int mode = (Integer) dispatchMethod.invoke(manager, new Object[]{24, Binder.getCallingUid(), context.getApplicationContext().getPackageName()});
        return AppOpsManager.MODE_ALLOWED == mode;
    } catch (Exception e) {
        return false;
    }
}

}

错误

  java.lang.RuntimeException: Unable to create service com.appmaster.akash.messageplus.Services.MainService: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@463b2ce -- permission denied for window type 2002
                                                                                 at android.app.ActivityThread.handleCreateService(ActivityThread.java:3414)
                                                                                 at android.app.ActivityThread.-wrap4(Unknown Source:0)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1683)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                                 at android.os.Looper.loop(Looper.java:164)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
                                                                              Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@463b2ce -- permission denied for window type 2002
                                                                                 at android.view.ViewRootImpl.setView(ViewRootImpl.java:789)
                                                                                 at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
                                                                                 at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:92)
                                                                                 at com.appmaster.akash.messageplus.Services.MainService.onCreate(MainService.java:83)
                                                                                 at android.app.ActivityThread.handleCreateService(ActivityThread.java:3404)
                                                                                 at android.app.ActivityThread.-wrap4(Unknown Source:0) 
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1683) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:105) 
                                                                                 at android.os.Looper.loop(Looper.java:164) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:6541) 
                                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                                 at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

此特定服务在较低的 API 中运行良好,但在 26 时遇到问题...我不太确定如何为服务授予权限,但仍然尝试过...所以有人可以帮我解决错误......它只是一项基本服务......同样在较低的API中,我也不知道如何直接从应用程序授予权限,所以我去设置并手动进行,然后它就可以工作了。因此,如果有人帮助我了解所有 API,那就太好了

【问题讨论】:

    标签: java android android-studio exception service


    【解决方案1】:

    您不能直接从应用授予此权限 - 根据设计,用户必须导航到系统/应用设置并在其中启用它。

    此外,由于 API 26 (Oreo) WindowManager.LayoutParams.TYPE_PHONE 也已被弃用,如果您的目标是 api 26 或更高版本,则会导致您的应用程序崩溃。您需要将其替换为WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY

    【讨论】:

    • 非常感谢朋友
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-06
    • 1970-01-01
    相关资源
    最近更新 更多