简体   繁体   English

应用程序因 Permission Denial 崩溃:不允许发送广播 android.intent.action.SCREEN_ON

[英]App crashes with Permission Denial: not allowed to send broadcast android.intent.action.SCREEN_ON

I am new to the broadcast receiver and implemented broadcast receiver dynamically as shown below我是广播接收器的新手,并动态实现了广播接收器,如下所示

IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    BroadcastReceiver mService=new StarterOnBoot();
    registerReceiver(mService,filter);

in manifest在清单中

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.autostartter">
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.AutoStartTer">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/Theme.AutoStartTer.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".RuntimePermission"
        android:label="@string/app_name"
        android:theme="@style/Theme.AutoStartTer.NoActionBar">
    </activity>
</application>

</manifest>

I have also created a button, by clicking who send broadcast manually as shown below我还创建了一个按钮,通过单击手动发送广播的人,如下所示

view.findViewById(R.id.button_second).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent=new Intent();
            intent.setAction(Intent.ACTION_SCREEN_ON);
            MainActivity.instance_main.sendBroadcast(intent);
        }
    });

Broadcast Service is广播服务是

package com.example.autostartter;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import java.util.logging.Handler;

public class StarterOnBoot extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "Broadcast Hit", Toast.LENGTH_SHORT).show();
    if(intent.getAction().equals(Intent.CATEGORY_HOME))
    {
        Intent i=new Intent(context,MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
    if(intent.getAction().equals(Intent.ACTION_USER_UNLOCKED))
    {
        Intent i=new Intent(context,MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
    if(intent.getAction().equals(Intent.ACTION_SCREEN_ON))
    {
        Intent i=new Intent(context,MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
    if(intent.getAction().equals(Intent.ACTION_USER_PRESENT))
    {
        Intent i=new Intent(context,MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
    if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
    {
        Intent i=new Intent(context,MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }
}
}

the broadcast works properly when the user unlocks the screen.当用户解锁屏幕时,广播正常工作。 However, when I click on the button the application crashes with the following exception但是,当我单击按钮时,应用程序崩溃并出现以下异常

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.autostartter, PID: 30645
java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.SCREEN_ON from pid=30645, uid=10245
    at android.os.Parcel.createException(Parcel.java:2074)
    at android.os.Parcel.readException(Parcel.java:2042)
    at android.os.Parcel.readException(Parcel.java:1990)
    at android.app.IActivityManager$Stub$Proxy.broadcastIntent(IActivityManager.java:5089)
    at android.app.ContextImpl.sendBroadcast(ContextImpl.java:1059)
    at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:458)
    at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:458)
    at com.example.autostartter.FirstFragment$2.onClick(FirstFragment.java:44)
    at android.view.View.performClick(View.java:7185)
    at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
    at android.view.View.performClickInternal(View.java:7162)
    at android.view.View.access$3500(View.java:819)
    at android.view.View$PerformClick.run(View.java:27684)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:224)
    at android.app.ActivityThread.main(ActivityThread.java:7562)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
 Caused by: android.os.RemoteException: Remote stack trace:
    at com.android.server.am.ActivityManagerService.broadcastIntentLocked(ActivityManagerService.java:15351)
    at com.android.server.am.ActivityManagerService.broadcastIntentLocked(ActivityManagerService.java:15202)
    at com.android.server.am.ActivityManagerService.broadcastIntent(ActivityManagerService.java:15991)
    at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:2051)
    at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2799)

I have also acquired overlay permissions我还获得了覆盖权限

Please ignore any mistake not directly related to this problem.请忽略与此问题不直接相关的任何错误。

Add this permission to the Manifest file.将此权限添加到清单文件。

<uses-permission android:name="android.permission.WRITE_SETTINGS" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 嘿,我才刚开始在android中进行编码并收到错误Permission Denial:不允许在android中发送广播 - Hey , I am just started to code in android and getting error Permission Denial: not allowed to send broadcast in android java.lang.SecurityException:权限被拒绝:启动 Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] - java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] 我的应用因拒绝权限而崩溃 - My App Crashes because of permission denial 使用广播接收器拒绝权限 - Permission Denial With Broadcast Receiver Android:java.lang.SecurityException:权限拒绝:开始发送邮件的意图 - Android: java.lang.SecurityException: Permission Denial: start Intent to send mail 应用程序崩溃引发权限拒绝异常:读取com.android.providers.media.MediaProvider - App Crashes throwing an exception of permission denial:reading com.android.providers.media.MediaProvider 应用随机崩溃,显示权限被拒绝 - App crashes randomly ,show's permission denial exception 显示 Android 自动化错误:“安全异常:权限拒绝:启动意图” - Android Automation Error Displayed: "Security exception: Permission Denial: starting Intent" 应用程序崩溃,并出现java.lang.SecurityException:权限被拒绝 - App crashes with java.lang.SecurityException: Permission Denial 返回应用前,Android ACTION_IMAGE_CAPTURE意向崩溃 - Android ACTION_IMAGE_CAPTURE intent crashes before returning to app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM