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" />
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.