开发一项服务,每当用户打开屏幕/用户在场/启动完成时,该服务就会启动。我在google上研究了一些,发现ACTION_SCREEN_ON只是以编程方式注册,所以我跟着:
private void registerAction() {
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_USER_PRESENT);
filter.addAction(Intent.ACTION_BOOT_COMPLETED)
AutoStart mReceiver = new AutoStart();
registerReceiver(mReceiver, filter);
}
并在第一个Activity的onCreate
方法中调用了该函数。起初,我的SAMSUNG设备上的一切正常。问题是当我在我的HTC和LG设备上测试我的应用程序时,没有发送广播信号,看起来接收器根本没有注册。所以我做了一点修改,我将接收器添加到清单文件中:
<receiver
android:name=".AutoStart"
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.intent.action.SCREEN_ON" />
</intent-filter>
</receiver>
现在我的HTC,SAMSUNG和LG设备都工作了。但是,在SAMSUNG上,广播信号发送了TWICE。所以清单文件上的接收器都是以编程方式注册的!?!关于这个问题的任何想法?为什么我的HTC和LG设备无法正常工作?
另一件事,看起来那些信号有时不起作用(我在SAMSUNG设备中单独测试),我不知道为什么。无论如何都要保证一直发送的广播信号?
这是接收者的代码:
public class AutoStart extends BroadcastReceiver {
private Context context;
private static final String TAG = "AutoStart";
@Override
public void onReceive(Context context, Intent intent) {
this.context = context;
DLog.d(TAG, "Auto start at any action screen on - user present - boot completed");
}
// more functions, not related to the question
}
希望我明确表示自己,并感谢任何帮助。如果需要任何信息,请留下评论。