简体   繁体   English

应用被杀死时,广播接收器未在Oreo上收听

[英]Broadcast receiver not listening on Oreo when app gets killed

my broadcast receiver is working fine on Pause() function but when i kill my app my broadcast receiver stops listening only in oreo but working fine on other API Levels less then oreo. 我的广播接收器在Pause()函数上运行良好,但是当我杀死我的应用程序时,我的广播接收器仅在oreo中停止监听,而在其他API级别(而不是oreo)上运行良好。 i also registered my broadcast receiver in my manifest file and also registered it on my resume(). 我还在清单文件中注册了我的广播接收器,并在我的履历()中注册了它。

My Main Activity Code: 我的主要活动代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startService(new Intent(this, BroadcastService.class));
    Log.i(TAG, "Started service");
}


@Override
public void onResume() {
    super.onResume();
    registerReceiver(receiver, new IntentFilter(BroadcastService.COUNTDOWN_BR));
    Log.i(TAG, "Registered broacast receiver");
}

Here is my Service class: 这是我的服务班级:

public class BroadcastService  extends Service {

    private final static String TAG = "BroadcastService";

    public static final String COUNTDOWN_BR = "com.codecollapse";
      Intent bi = new Intent(COUNTDOWN_BR);

    CountDownTimer cdt = null;

    @Override
    public void onCreate() {
        super.onCreate();

        Log.i(TAG, "Starting timer...");

        cdt = new CountDownTimer(30000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {

                Log.i(TAG, "Countdown seconds remaining: " + millisUntilFinished / 1000);
                bi.putExtra("countdown", millisUntilFinished);
                //sendBroadcast(bi);
                sendBroadcast(new Intent(getBaseContext(), CountDownReceiver.class).setAction("com.codecollapse").putExtra("countdown",millisUntilFinished));
            }

            @Override
            public void onFinish() {
                bi.putExtra("IsFinish",true);
                Log.i(TAG, "Timer finished");
            }
        };

        cdt.start();
    }

    @Override
    public void onDestroy() {

        cdt.cancel();
        Log.i(TAG, "Timer cancelled");
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
}

Here is my receiver class 这是我的接收器课程

public class CountDownReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "You Poke the service", Toast.LENGTH_SHORT).show();

    }

}

Here is my manifest file: 这是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidservices">
<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>
    <receiver
        android:name=".CountDownReceiver">
        <intent-filter>
            <action android:name="com.codecollapse"/>
        </intent-filter>
    </receiver>
    <service android:name=".BroadcastService" />
</application>

Hi You can read the topic as announced with Oreo 8.0 release: 嗨,您可以阅读Oreo 8.0版本中宣布的主题:

在此处输入图片说明

you can see the detail of it at official link . 您可以在官方链接上看到它的详细信息。

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

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