kum*_*ail 6 java android broadcastreceiver android-intent
所以我制作了两个不同的应用程序,一个发送广播,另一个接收广播并显示祝酒词。但是,当我关闭接收器应用程序时,即使我在清单文件中定义了接收器,第二个应用程序也不再接收广播。
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");
}
});
}
Run Code Online (Sandbox Code Playgroud)
应用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");
}
Run Code Online (Sandbox Code Playgroud)
应用 2s 清单:
<?xml version="1.0" encoding="utf-8"?>
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)
Med*_*der 13
在清单中静态注册我的 BroadcastReceiver (BR)、应用适当的意图过滤器、使用 JobIntentService(并将其注册在清单中)来处理从我的 BR 调用的工作后,我仍然得到不一致的结果。
完成我上面列出的所有操作后,即使应用程序关闭,您也应该能够发送 ADB 命令来激活您的广播并处理您的服务中的工作。这在某些时候对我有用,但不是所有时间。
本文描述了对 BR 的限制。“从 Android 3.1开始,如果用户从未启动过相应的应用程序,或者用户通过 Android 菜单明确停止了应用程序,则默认情况下,Android 系统会将所有接收器排除在接收意图之外”(AKA 用户执行 Force Stop)
当我通过调试启动应用程序,然后在我的设备上关闭它时,我的 ADB 命令永远不会激活我的 BR。但是,在调试会话结束后,当我在设备上打开应用程序并将其关闭时,我可以通过 ADB 命令激活我的 BR。
发生这种情况是因为当您调试应用程序,然后在设备上手动将其关闭时,Android 认为这是强制停止,因此为什么我的 BR 无法激活,直到我在没有调试的情况下重新打开设备上的应用程序。
在互联网上搜索了几个小时,但找不到我的解决方案,所以我想我会在这里发布它,以防万一一些可怜的不幸的人遇到了与我相同的奇怪功能。
快乐编码:)
小智 4
首先,您需要使用该服务才能使此功能发挥作用。
在活动中,您可以使用以下代码启动和停止服务。
// to start a service
Intent service = new Intent(context, MyBrodcastRecieverService.class);
context.startService(service);
// to Stop service
Intent service = new Intent(context, MyBrodcastRecieverService.class);
context.stopService(service);
Run Code Online (Sandbox Code Playgroud)
然后您就可以使用以下服务
public class MyBrodcastRecieverService 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);
}
}
Run Code Online (Sandbox Code Playgroud)