Android的广播接收器不会被调用 [英] Android BroadcastReceiver is never called
问题描述
我想要实现开/关处理屏幕的服务。要做到这一点,我已经创建 BootReceiver
被称为在引导完成,在我开始我的服务。然而,我的 ScreenBroadcastReceiver
永远不会被调用,为什么呢?难道我在错误的地方注册,我做到了的onCreate服务的......请大家帮帮我,我要注册呢?
I want to implement a service that handle screen on/off. To do so, I have created BootReceiver
that is called at boot completed and within I start my service. However, my ScreenBroadcastReceiver
is never called, why? Did I register it in the wrong place, i did it onCreate of the service... Please help me where should I register it?
因此,应用程序应该在背景(无用户交互)运行,并且它将记录在屏幕上和关闭活动。我已经开始这些codeS,但似乎我需要注册我的ScreenBroadcastReceiver广播接收器。如果我这样做的onCreate的服务。我应该改变呢?我的最终目标是使应用程序的工作,而无需用户交互运行的背景这个程序
So, the application should run on the background (no user interaction) and it will log the screen on and off activities. I have started these codes, but it seems that I need to register my ScreenBroadcastReceiver broadcastreceiver. If I do it onCreate of the service. should i change it ? my end goal is to run this app on background so that the application works without user interaction
这是BootReceiver被称为最开始:
This is BootReceiver that is called in the very beginning:
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.w("TAG", "BootReceiver");
Intent service = new Intent(context, ScreenListenerService.class);
context.startService(service);
}
}
这是该服务:
public class ScreenListenerService extends Service {
public void onCreate(){
super.onCreate();
Log.w("TAG", "ScreenListenerService---OnCreate ");
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver screenOnReceiver = new ScreenBroadcastReceiver();
registerReceiver(screenOnReceiver, filter);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.w("TAG", "ScreenListenerService---onStart ");
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
这是ScreenBroadcastReceiver:
This is the ScreenBroadcastReceiver:
public class ScreenBroadcastReceiver extends BroadcastReceiver {
///this part is never called, even if the screen is turned on/off.
public static boolean screenOff;
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
screenOff = true;
System.out.println("SCREEN TURNED OFF on BroadcastReceiver");
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
screenOff = false;
System.out.println("SCREEN TURNED ON on BroadcastReceiver");
}
Intent i = new Intent(context, ScreenListenerService.class);
i.putExtra("screen_state", screenOff);
context.startService(i);
}
}
和清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".ScreenListenerService"></service>
<receiver android:name=".BootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE" />
</intent-filter>
</receiver>
</application>
</manifest>
更新:
人们可以在模拟器中测试该code,让我知道结果呢?
Update: Can one test this code in the emulator and let me know the result ?
推荐答案
填写您的广播接收器
在在onStart()
法
@Override
public void onStart(Intent intent, int startId) {
try {
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
} catch (Exception e) {
}
}
这篇关于Android的广播接收器不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!