简体   繁体   English

Android-服务中的广播接收器无法接收

[英]Android - Broadcast Receiver in Service, does not receive

I am using AlarmManager in order to set alarm. 我正在使用AlarmManager来设置警报。 I have a Service, and in that Service I have a Broadcast Receiver. 我有一个服务,在该服务中,我有一个广播接收器。

The problem is, when selected time and date comes, receiver cannot receive it. 问题是,当选定的时间和日期到来时,接收者无法接收到它。

onStart part of my Service class, and where is register my receiver: 我的Service类的onStart部分,在哪里注册我的接收者:

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 Method for receiver: 接收者的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!"); ...}

Part that I run service and set alarm with AlarmManager: 我运行服务并使用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: 表现:

<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>

What am I missing? 我想念什么?

I am trying this since I want receive it even user closes the app. 我正在尝试此操作,因为即使用户关闭应用程序也希望收到它。

If the user force-close your app, there is nothing you can do to make your receiver run, other than the user restarting your app manually. 如果用户强行关闭您的应用程序,则除了用户手动重新启动应用程序外,您无法执行任何操作来使接收器运行。

Other than that, please make sure that you added the BOOT_COMPLETED action to your intent filter, as well as the RECEIVE_BOOT_COMPLETED permission to your manifest, as described in this thread. 除此之外,请确保已将BOOT_COMPLETED操作添加到您的意图过滤器,并将RECEIVE_BOOT_COMPLETED权限添加到清单中,如本线程中所述。 This will let your receiver start up again after the phone has been rebooted (turned on and off again). 手机重启后(打开和关闭),这将使您的接收器再次启动。

Android BOOT_COMPLETED not received when application is closed 关闭应用程序后未收到Android BOOT_COMPLETED

Furthermore, on this thread it has been mentioned that in order for your receiver to work after the app has been closed (not force-closed), the receiver needs to be registered in the manifest: 此外,在此线程上已经提到过,为了使您的接收器在关闭应用程序(而非强制关闭)之后工作,接收器需要在清单中注册:

Is there a way to keep a repeating alarm working after existing the app that uses a broadcast receiver? 现有使用广播接收器的应用程序存在后,是否有办法使重复警报正常工作?

Give it a try and let us know if it helped you :) 试试看,让我们知道它是否对您有帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM