Android8 自定義廣播接收不到的問題

最近在用安卓廣播的時候,按照流程進行操做,但是無論怎樣都沒有出現我接受的廣播,網上查閱資料之後,發如今Android8中,若是是靜態註冊廣播,須要在action中保留原來的靜態廣播,加入Component參數;代碼以下:java

AndroidMainifest.xml:android

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.administrator.myapplication">
    <!-- 權限 -->
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <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=".BroadcastActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"></action>
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".MyBroadcastReceiver"
            android:enabled="true"
            android:exported="true"
            >
            <intent-filter>
                <action android:name="com.example.administrator.myapplication.MyBroadcastReceiver"/>
            </intent-filter>
        </receiver>
    </application>
</manifest>

  

MyBroadcastReceiver.java:
package com.example.administrator.myapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

public class MyBroadcastReceiver  extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "收到廣播了", Toast.LENGTH_LONG).show();
    }
}

  

BroadcastActivity.java:
package com.example.administrator.myapplication;

import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class BroadcastActivity extends AppCompatActivity {
    private MyBroadcastReceiver myBroadcastReceiver;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broadcast);

        //註冊廣播
        myBroadcastReceiver = new MyBroadcastReceiver();
        IntentFilter itFilter = new IntentFilter();
        Button btn = (Button) findViewById(R.id.sendBtn);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent("com.example.administrator.myapplication.MyBroadcastReceiver");
                //第一個參數爲包的路徑,第二個參數爲類名
                intent.setComponent(new ComponentName("com.example.administrator.myapplication", "com.example.administrator.myapplication.MyBroadcastReceiver"));
                sendBroadcast(intent);
            }
        });
    }

    //註銷的時候取消廣播
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myBroadcastReceiver);
    }
}

 

 

相關文章
相關標籤/搜索