Android - 服务中的广播接收器,不接收
Android - Broadcast Receiver in Service, does not receive
我正在使用 AlarmManager 来设置警报。
我有一个服务,在那个服务中我有一个广播接收器。
The problem is, when selected time and date comes, receiver cannot receive it.
onStart 我服务的一部分 class,我的接收器在哪里注册:
public int onStartCommand(Intent intent, int flags, int startId) {
super.onCreate();
IntentFilter intentFilter = new IntentFilter();
registerReceiver(receiver, intentFilter);
Log.i("com.example.adama", "Service has started!");
return START_STICKY;
}
接收方的 onReceive 方法:
public static BroadcastReceiver receiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i("com.example.adama", "Receiver has received!"); ...}
我 运行 使用 AlarmManager 服务和设置警报的部分:
Intent serviceIntent = new Intent(this, BrService.class);
startService(serviceIntent);
Intent intent = new Intent(this, BrService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,alarmCode++, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.adama.wifiv2_spinner">
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-feature android:name="android.hardware.wifi" />
<uses-permission android:name="android.permission.VIBRATE"/>
<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">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".BrService"/>
</application>
我错过了什么?
我正在尝试这个,因为即使用户关闭应用程序我也想收到它。
如果用户 force-close 您的应用程序,除了用户手动重启您的应用程序之外,您无法做任何事情来让您的接收器 运行。
除此之外,请确保您已将 BOOT_COMPLETED 操作添加到您的 intent 过滤器中,并已将 RECEIVE_BOOT_COMPLETED 权限添加到您的清单中,如本帖中所述。这将使您的接收器在 phone 重新启动(再次打开和关闭)后再次启动。
Android BOOT_COMPLETED not received when application is closed
此外,在此线程中提到,为了让您的接收器在应用程序关闭后工作(不是 force-closed),接收器需要在清单中注册:
试一试,如果对您有帮助,请告诉我们:)
我正在使用 AlarmManager 来设置警报。 我有一个服务,在那个服务中我有一个广播接收器。
The problem is, when selected time and date comes, receiver cannot receive it.
onStart 我服务的一部分 class,我的接收器在哪里注册:
public int onStartCommand(Intent intent, int flags, int startId) {
super.onCreate();
IntentFilter intentFilter = new IntentFilter();
registerReceiver(receiver, intentFilter);
Log.i("com.example.adama", "Service has started!");
return START_STICKY;
}
接收方的 onReceive 方法:
public static BroadcastReceiver receiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i("com.example.adama", "Receiver has received!"); ...}
我 运行 使用 AlarmManager 服务和设置警报的部分:
Intent serviceIntent = new Intent(this, BrService.class);
startService(serviceIntent);
Intent intent = new Intent(this, BrService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,alarmCode++, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.adama.wifiv2_spinner">
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-feature android:name="android.hardware.wifi" />
<uses-permission android:name="android.permission.VIBRATE"/>
<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">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".BrService"/>
</application>
我错过了什么?
我正在尝试这个,因为即使用户关闭应用程序我也想收到它。
如果用户 force-close 您的应用程序,除了用户手动重启您的应用程序之外,您无法做任何事情来让您的接收器 运行。
除此之外,请确保您已将 BOOT_COMPLETED 操作添加到您的 intent 过滤器中,并已将 RECEIVE_BOOT_COMPLETED 权限添加到您的清单中,如本帖中所述。这将使您的接收器在 phone 重新启动(再次打开和关闭)后再次启动。
Android BOOT_COMPLETED not received when application is closed
此外,在此线程中提到,为了让您的接收器在应用程序关闭后工作(不是 force-closed),接收器需要在清单中注册:
试一试,如果对您有帮助,请告诉我们:)