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

时间:2019-07-31 05:47:02

标签: java android broadcastreceiver

我的广播接收器在Pause()函数上运行良好,但是当我杀死我的应用程序时,我的广播接收器仅在oreo中停止监听,而在其他API级别(而不是oreo)上运行良好。我也将广播接收器注册到清单文件中,也将其注册到我的resume()中。

我的主要活动代码:

@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");
}

这是我的服务班级:

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

这是我的接收者课程

public class CountDownReceiver extends BroadcastReceiver {

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

    }

}

这是我的清单文件:

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

1 个答案:

答案 0 :(得分:0)

嗨,您可以阅读Oreo 8.0版本中宣布的主题:

enter image description here

您可以在official link上查看它的详细信息。

相关问题