静态注册BroadcastReceiver没有响应

时间:2022-11-18 23:14:15
我写了一个很简单的例子。可就是没有反应。找了半天也找不到哪里出了错误。希望大家帮忙看看。。旁观者清啊。。谢谢。

MainActivity.java

public class MainActivity extends Activity {
private static final String INTENAL_ACTION_1 = "su1.bluetooth.true";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Intent intent = new Intent(INTENAL_ACTION_1);
        sendBroadcast(intent);
    }
}


BroadcastRecTest.java

public class BroadcastRecTest extends BroadcastReceiver{
private static final String INTENAL_ACTION_1="su1.bluetooth.true";
private static final String TAG = "BroadcastRecTest";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals(INTENAL_ACTION_1)){
Log.d(TAG,"1231321323");
Toast.makeText(context, action, 0).show();

}

}

}


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.huawei.biz"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <reciver android:name="BroadcastRecTest">
            <intent-filter>
                <action android:name="su1.bluetooth.true"/>
            </intent-filter>
        </reciver>
    </application>


</manifest>

3 个解决方案

#1


你这样写一下看看有响应不?

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.huawei.biz"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <reciver android:name="BroadcastRecTest">
            <intent-filter>
                <action android:name="su1.bluetooth.true"/>
            </intent-filter>
    </reciver>
</manifest>

#2


<reciver android:name="BroadcastRecTest">

<receiver></receiver>

写错啦...

#3


这。。。我去。。。。果然是旁观者清。。。。谢谢了。。。汗

#1


你这样写一下看看有响应不?

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.huawei.biz"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <reciver android:name="BroadcastRecTest">
            <intent-filter>
                <action android:name="su1.bluetooth.true"/>
            </intent-filter>
    </reciver>
</manifest>

#2


<reciver android:name="BroadcastRecTest">

<receiver></receiver>

写错啦...

#3


这。。。我去。。。。果然是旁观者清。。。。谢谢了。。。汗