Android - How to trigger a Broadcast Receiver to c

2019-04-03 22:33发布

I have scheduled alarm for my application.

I have implemented broadcast receiver to be triggered once the alarm time reaches.

How to manually call broadcast receiver to execute the code inside of onReceive method without replicating the code twice.

I thought of having the code in utility singleton call and call that method by having util class instance from anywhere.

But is that any other way to call that onReceive method directly or else broadcast intent problematically.

android:exported="false" //Additional parameter of receiver when defining in manifest file.

Another question is what is that exported parameter means. Please help me to understand this.

4条回答
家丑人穷心不美
2楼-- · 2019-04-03 22:42

How to manually call broadcast receiver to execute the code inside of onReceive method without replicating the code twice.

Fire BroadcastReceiver using sendBroadcast same action which added in AndroidManifest.xml :

Intent intent=new Intent(CUSTOM_ACTION_STRING);
// Add data in Intent using intent.putExtra if any required to pass 
sendBroadcast(intent);

what is that android:exported parameter means

As in android:exported doc : Whether or not the broadcast receiver can receive messages from sources outside its application — "true" if it can, and "false" if not

Means if:

android:exported=true: other application also able to fire this broadcast receiver using action

android:exported=false: other application not able to fire this broadcast receiver using action

查看更多
欢心
3楼-- · 2019-04-03 22:45

You need to mention the action which is required to be filter by Android OS to notify you. i.e.: inside manifest file,

<receiver
android:name="com.example.MyReceiver"
android:enabled="true" >
<intent-filter>
    <action android:name="com.example.alarm.notifier" />//this should be unique string as action
</intent-filter>

and

whenever you want to call broadcast receiver's onReceive method,

Intent intent = new Intent();
intent.setAction("com.example.alarm.notifier");
sendBroadcast(intent);
查看更多
Anthone
4楼-- · 2019-04-03 22:49

Here is a more type-safe solution:

  • AndroidManifest.xml:

    <receiver android:name=".CustomBroadcastReceiver" />
    
  • CustomBroadcastReceiver.java

    public class CustomBroadcastReceiver extends BroadcastReceiver
    {
        @Override
        public void onReceive(Context context, Intent intent)
        {
            // do work
        }
    }
    
  • *.java

    Intent i = new Intent(context, CustomBroadcastReceiver.class);
    context.sendBroadcast(i);
    
查看更多
等我变得足够好
5楼-- · 2019-04-03 22:54

1. The way to launch a BroadcastReceiver manually is by calling

Intent intent = new Intent("com.myapp.mycustomaction");
sendBroadcast(intent);

where "com.myapp.mycustomaction" is the action specified for your BroadcastReceiver in the manifest. This can be called from an Activity or a Service.

2. It is known that Android allows applications to use components of other applications. In this way, Activitys, Services, BroadcastReceivers and ContentProviders of my application can be started by external applications, provided that the attribute android:exported = true is set in the manifest. If it is set to android:exported = false, then this component cannot be started by an external application. See here.

查看更多
登录 后发表回答