Ale*_*lex 7 android android-intent android-broadcast
我正在尝试制作一个听取wifi改变广播并做一些动作的Android应用程序.但问题是当进程被杀死时它无法正常工作.我发现这个问题说没有活动就无法工作
如何在没有Activity/Service的情况下创建BroadcastReceiver?
所以这不是另类.因此我创建了一个空的活动.我不想创建一个在后台运行的服务.如果应用程序被杀死,我怎样才能让我的应用程序继续听.我已经在清单中注册了广播.
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.background.service.BroadCastReceiver">
<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
这是我的班级
public class BroadCastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//do some action
}
}
Run Code Online (Sandbox Code Playgroud)
通常,如果应用程序被杀死并且广播到来,您的应用程序进程将由系统为您启动。绝对没有必要让某些东西保持活动状态或进行任何额外的操作(否则BroadcastReceiver,如果您可以使用,为什么需要Service?)
| 归档时间: |
|
| 查看次数: |
10528 次 |
| 最近记录: |