Android 为什么onReceive方法不是';当我的手机屏幕关闭时,你没有打电话吗?
我正在调试我的应用程序,我遇到了一个有趣的问题。我在我的Android 为什么onReceive方法不是';当我的手机屏幕关闭时,你没有打电话吗?,android,Android,我正在调试我的应用程序,我遇到了一个有趣的问题。我在我的onReceive方法中有一个断点,只有当我的手机屏幕打开(唤醒)时,程序才停止在那里 为什么呢 如果您需要我的代码: private void gcmInit() { mRegistrationProgressBar = (ProgressBar) findViewById(R.id.registrationProgressBar); mRegistrationBroadcastReceiver = new Broad
onReceive
方法中有一个断点,只有当我的手机屏幕打开(唤醒)时,程序才停止在那里
为什么呢
如果您需要我的代码:
private void gcmInit() {
mRegistrationProgressBar = (ProgressBar) findViewById(R.id.registrationProgressBar);
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
mRegistrationProgressBar.setVisibility(ProgressBar.GONE);
SharedPreferences sharedPreferences =
PreferenceManager.getDefaultSharedPreferences(context);
boolean sentToken = sharedPreferences
.getBoolean(QuickstartPreferences.SENT_TOKEN_TO_SERVER, false);
if (sentToken) {
// mInformationTextView.setText(getString(R.string.gcm_send_message));
} else {
// mInformationTextView.setText(getString(R.string.token_error_message));
}
}
};
if (checkPlayServices()) {
// Start IntentService to register this application with GCM.
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
}
}
另外,我在主要活动中也有以下两种方法:
@Override
protected void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
new IntentFilter(QuickstartPreferences.REGISTRATION_COMPLETE));
}
@Override
protected void onPause() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver);
super.onPause();
}
我的舱单,我在其中登记接收人:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<!-- GCM START -->
<receiver
android:name="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.example.android.bluetoothchat" />
</intent-filter>
</receiver>
<service
android:name="com.example.android.gcm.MyGcmListenerService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
<service
android:name="com.example.android.gcm.MyInstanceIDListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.gms.iid.InstanceID"/>
</intent-filter>
</service>
<service
android:name="com.example.android.gcm.RegistrationIntentService"
android:exported="false">
</service>
<!-- GCM END -->
<activity
android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="portrait"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".DeviceListActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/select_device"
android:theme="@android:style/Theme.Holo.Dialog"/>
</application>
您实例化了一个接收者,但从未将其注册到您的上下文中。使用context.registerReceiver()方法。谢谢。我应该使用什么作为IntentFilter参数?我正在尝试:this.registerReceiver(mrRegistrationBroadcastReceiver,new IntentFilter(getCallingPackage());您的意图过滤器应该具体指定要使用的过滤器。您肯定不希望broadcastReceiver为每个Android目的调用onReceive
方法。您可以指定要作为意图筛选器挂接到的特定意图操作。此外,如果您使用的是this.registerReceiver()
,则接收器将在活动的上下文中运行,因此如果您的活动不在屏幕上,接收器将不会触发。如果它正在执行需要后台任务的任务,即使您的活动不在屏幕上,也要在清单中声明它,而不是通过代码执行。我很高兴您能够解决您的问题!我看不到你的onResume()onPause()
方法,但你是在onPause()
上注销接收器的(当屏幕关闭且设备空闲时调用),这就是为什么在屏幕关闭时接收器没有运行的原因。