所以我制作了两个不同的应用程序,一个发送广播,另一个接收它并显示一个吐司。但是,当我关闭接收器应用程序时,即使我在清单文件中定义了接收器,第二个应用程序也不再接收广播。
app1的MainActivity中的广播发送者。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button)findViewById(R.id.button2);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent();
i.setAction("com.example.ali.rrr");
i.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
sendBroadcast(i);
Log.e("Broadcast","sent");
}
});
}
App 2广播接收器:
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Toast.makeText(context, "Broadcast has been recieved!", Toast.LENGTH_SHORT).show();
Log.e("SUCCESS", "IN RECIEVER");
//throw new UnsupportedOperationException("Not yet implemented");
}
App 2s Manifest:
<?xml version="1.0" encoding="utf-8"?>
<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/AppTheme">
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.ali.rrr" />
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".Main2Activity"
android:label="@string/title_activity_main2"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
答案 0 :(得分:8)
在清单中静态注册我的BroadcastReceiver(BR),应用适当的意图过滤器,使用JobIntentService(并将其注册在清单中)处理从我的BR调用的工作之后,我仍然得到不一致的结果。 / p>
完成上述所有操作后,即使该应用已关闭,您也应该能够发送ADB命令来激活广播并处理服务中的工作。有时候,这对我有用,但并非一直如此。
This article描述了对BR的限制。 “从Android 3.1开始,如果用户从未启动过相应的应用程序,或者用户通过Android菜单明确停止了该应用程序,则默认情况下,Android系统会将所有接收者排除在接收意图之外”( >又是用户执行强制停止)
当我通过调试启动应用程序,然后在设备上将其关闭时,我的ADB命令从不激活我的BR。但是,调试会话结束后,当我打开设备上的应用程序并在关闭状态下滑动时,可以通过ADB命令激活我的BR。
发生这种情况的原因是,当您调试应用程序时,在设备上手动将其关闭时会滑动它, Android认为这是强制停止,因此为什么无法激活我的BR,直到我重新打开设备上的应用程序而无需调试。
使互联网清理了好几个小时,无法找到我的解决方案,所以我想把它发布在这里,以防万一某个不幸的不幸灵魂遇到了与以前相同的奇怪功能。
快乐编码:)
答案 1 :(得分:1)
First of all you need to use the Service for this functionality to work.
In the Activity you can start and stop the service by using the below codes.
//to start a service
Intent service = new Intent(context, MyBrodcastRecieverService.class);
context.startService(service);
//to Stop service
Intent service = new Intent(context, MyBrodcastRecieveService.class);
context.stopService(service);
Then you can use the below service
public class MyBrodcastRecieveService extends Service
{
private static BroadcastReceiver br_ScreenOffReceiver;
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
@Override
public void onCreate()
{
registerScreenOffReceiver();
}
@Override
public void onDestroy()
{
unregisterReceiver(br__ScreenOffReceiver);
m_ScreenOffReceiver = null;
}
private void registerScreenOffReceiver()
{
br_ScreenOffReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
Log.d(TAG, "ACTION_SCREEN_OFF");
// do something, e.g. send Intent to main app
}
};
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
registerReceiver(br_ScreenOffReceiver, filter);
}
}
答案 2 :(得分:0)
试试这种方式..
Intent i = new Intent();
i.setAction("com.example.ali.rrr");
i.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
i.setComponent(
new ComponentName("PackageNameApp2","PackageNameApp2.MainActivity"));
sendBroadcast(i);
答案 3 :(得分:0)
您可以通过以下解决方案;
Activity.java
Intent intent=new Intent(MainActivity.this,BroadcastService.class);
startService(intent);
BroadcastService.java
public class BroadcastService extends Service {
private static MusicIntentReceiver br_ScreenOffReceiver;
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
@Override
public void onCreate()
{
registerScreenOffReceiver();
}
@Override
public void onDestroy()
{
}
private void registerScreenOffReceiver()
{
br_ScreenOffReceiver = new MusicIntentReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
int state = intent.getIntExtra("state", -1);
switch (state) {
case 0:
Log.e("AAAAAAAAAA", "Headset is unplugged");
break;
case 1:
Log.e("AAAAAAAAA", "Headset is plugged");
break;
default:
Log.e("AAAAAAAAAAAA", "I have no idea what the headset state is");
}
}
}
};
IntentFilter filter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
registerReceiver(br_ScreenOffReceiver, filter);
}
}
证据
<service android:enabled="true" android:name=".BroadcastService" />