应用关闭后,广播接收器无法正常工作

编程入门 行业动态 更新时间:2024-10-10 00:20:58
本文介绍了应用关闭后,广播接收器无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所以我制作了两个不同的应用程序,一个发送广播,另一个接收它并显示吐司.但是,当我关闭接收方应用程序时,即使我在清单文件中定义了接收方,第二个应用程序也不再接收广播.

So I have two different apps made, one sends a broadcast and another receives it and displays a toast. However, when I close the receiver app the broadcast is no longer received by the second app even though I defined the receiver in the manifest file.

app1的MainActivity中的广播发送者.

The broadcast sender in the MainActivity of app1.

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广播接收器:

App 2 broadcast receiver:

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>

推荐答案

首先,您需要使用服务才能使用此功能.

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); } }

更多推荐

应用关闭后,广播接收器无法正常工作

本文发布于:2023-11-27 10:01:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637611.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:接收器   无法正常   工作

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!