应用程序关闭时广播接收器不工作
Broadcast receiver not working when app is closed
所以我制作了两个不同的应用程序,一个发送广播,另一个接收广播并显示祝酒词。但是,当我关闭接收器应用程序时,即使我在清单文件中定义了接收器,第二个应用程序也不再接收广播。
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");
}
});
}
应用 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 清单:
<?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>
试试这个方法..
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);
首先,您需要使用该服务才能使用此功能。
在Activity中,您可以使用以下代码启动和停止服务。
// 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);
那么您可以使用以下服务
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);
}
}
您可以通过以下解决方案;
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" />
在清单中静态注册我的 BroadcastReceiver (BR)、应用适当的意图过滤器、使用 JobIntentService(并在清单中注册)处理从我的 BR 调用的工作后,我仍然得到不一致的结果.
完成上面列出的所有操作后,您应该能够发送 ADB 命令来激活您的广播并处理服务中的工作,即使应用程序已关闭也是如此。这在某些时候对我有用,但并非总是如此。
This article 描述了对 BR 的限制。
"As of Android 3.1 the Android system excludes all receiver from receiving intents by default if the corresponding application has never been started by the user or if the user explicitly stopped the application via the Android menu"(又名用户执行强制停止)
当我通过调试启动应用程序,然后在我的设备上将其滑动关闭时,我的 ADB 命令从未激活我的 BR。但是,在我的调试会话结束后,当我在我的设备上打开应用程序并将其滑动关闭时,我可以通过 ADB 命令激活我的 BR。
发生这种情况是因为当您调试应用程序时,然后在设备上手动滑动关闭它,Android 认为这是强制停止 因此我的 BR 无法激活的原因直到我在没有调试的情况下在设备上重新打开应用程序。
在互联网上搜索了几个小时,但没能找到我的解决方案,所以我想我会 post 把它放在这里,以防万一一些不幸的人遇到了和我一样奇怪的功能。
编码愉快:)
我最近遇到了这个问题。 BroadcastReceiver 工作正常,即使该应用程序已从模拟器和三星手机的后台删除。但是它无法在 Realme、Mi 等中国制造的手机上启动我的应用程序。在努力寻找解决此问题的方法时,我发现在应用程序详细信息页面中有电池优化设置,其中 Auto-launch 功能被禁用。在我启用它之后,该应用程序运行良好,BroadcastReceiver 能够启动该应用程序。我无法找到以编程方式启用此设置的方法,但我发现了这个 ,它帮助我将用户引导至该设置页面。
所以我制作了两个不同的应用程序,一个发送广播,另一个接收广播并显示祝酒词。但是,当我关闭接收器应用程序时,即使我在清单文件中定义了接收器,第二个应用程序也不再接收广播。
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");
}
});
}
应用 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 清单:
<?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>
试试这个方法..
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);
首先,您需要使用该服务才能使用此功能。
在Activity中,您可以使用以下代码启动和停止服务。
// 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);
那么您可以使用以下服务
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);
}
}
您可以通过以下解决方案;
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" />
在清单中静态注册我的 BroadcastReceiver (BR)、应用适当的意图过滤器、使用 JobIntentService(并在清单中注册)处理从我的 BR 调用的工作后,我仍然得到不一致的结果.
完成上面列出的所有操作后,您应该能够发送 ADB 命令来激活您的广播并处理服务中的工作,即使应用程序已关闭也是如此。这在某些时候对我有用,但并非总是如此。
This article 描述了对 BR 的限制。 "As of Android 3.1 the Android system excludes all receiver from receiving intents by default if the corresponding application has never been started by the user or if the user explicitly stopped the application via the Android menu"(又名用户执行强制停止)
当我通过调试启动应用程序,然后在我的设备上将其滑动关闭时,我的 ADB 命令从未激活我的 BR。但是,在我的调试会话结束后,当我在我的设备上打开应用程序并将其滑动关闭时,我可以通过 ADB 命令激活我的 BR。
发生这种情况是因为当您调试应用程序时,然后在设备上手动滑动关闭它,Android 认为这是强制停止 因此我的 BR 无法激活的原因直到我在没有调试的情况下在设备上重新打开应用程序。
在互联网上搜索了几个小时,但没能找到我的解决方案,所以我想我会 post 把它放在这里,以防万一一些不幸的人遇到了和我一样奇怪的功能。
编码愉快:)
我最近遇到了这个问题。 BroadcastReceiver 工作正常,即使该应用程序已从模拟器和三星手机的后台删除。但是它无法在 Realme、Mi 等中国制造的手机上启动我的应用程序。在努力寻找解决此问题的方法时,我发现在应用程序详细信息页面中有电池优化设置,其中 Auto-launch 功能被禁用。在我启用它之后,该应用程序运行良好,BroadcastReceiver 能够启动该应用程序。我无法找到以编程方式启用此设置的方法,但我发现了这个