简体   繁体   中英

Android BroadcastReceiver error

Hello i create one broadcast test but no work

Manifest:

<receiver android:name=".BeaconsBroadcast"
        android:exported="false">
        <intent-filter>
            <action android:name="com.example.android.kontacktestbeacons.BeaconsBroadcast"/>
        </intent-filter>
    </receiver>

in my MainActivity:

protected void onStop() {
        super.onStop();
        try{

            Log.e("ENTRO ","ENTRO");
            Intent i = new Intent();
            i.setAction("com.example.android.kontacktestbeacons.BeaconsBroadcast");

            startService(i);
        }catch (Exception e){Log.e("ERROR","ERRR");}

    }

mi broadcast class:

public class BeaconsBroadcast extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        Log.e("ENTRO ","EBTROPOOOOO RECIVE");
        Toast.makeText(context, "Se ha pulsado el botón.", Toast.LENGTH_SHORT)
                .show();
    }
}

in logcat:

12-02 11:12:59.551  28588-28588/com.mydomain.myapplication W/ContextImpl﹕ Implicit intents with startService are not safe: Intent { act=com.example.android.kontacktestbeacons.BeaconsBroadcast } android.content.ContextWrapper.startService:494 com.example.android.kontacktestbeacons.MainActivity.onStop:101 android.app.Instrumentation.callActivityOnStop:1235
12-02 11:12:59.552     927-2008/? W/ActivityManager﹕ Unable to start service Intent { act=com.example.android.kontacktestbeacons.BeaconsBroadcast } U=0: not found

where my error?

You are declaring BeaconsBroadcast as a BroadcastReceiver but are using startService(i) to invoke it which is causing the issue.
You need to use the sendBroadcast(i) function to send a broadcast to a BroadcastReceiver. Also Since you have set android:exported="false" for BroadcastReceiver use

LocalBroadcastManager.getInstance(Context context).sendBroadcast(Intent)

Hello im fine by manifest

<receiver android:name="BeaconsBroadCast">
            <intent-filter>
                <action android:name="com.tutorialspoint.CUSTOM_INTENT">
                </action>
            </intent-filter>
        </receiver>

in application tag

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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