你好,我創建一個廣播試驗,但沒有工作Android的廣播接收器錯誤
清單:
<receiver android:name=".BeaconsBroadcast"
android:exported="false">
<intent-filter>
<action android:name="com.example.android.kontacktestbeacons.BeaconsBroadcast"/>
</intent-filter>
</receiver>
在我的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");}
}
英里廣播類:
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();
}
}
在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
我的錯誤在哪裏?
你好,我使用this.sendBroadcast(i);沒有標記錯誤,但沒有工作:/ mi class BeaconsBroacast不執行:/ – Luis 2014-12-02 17:39:39
使用LocalBroadcastManager.getInstance(this).sendBroadcast(i),因爲您已將其定義爲android:exported =「false」,因此只需要本地廣播。 – 2014-12-02 17:42:47
我有@Override 保護無效onStop(){ super.onStop(); 嘗試Log.e(「ENTRO」,「ENTRO」); Intent i = new Intent(); i.setAction(「com.example.android.kontacktestbeacons.BeaconsBroadcast」); LocalBroadcastManager.getInstance(this).sendBroadcast(i); }趕上(例外五){Log.e( 「ERROR」, 「ERRR」);} } – Luis 2014-12-02 17:56:25