2014-12-02 73 views
0

你好,我创建一个广播试验,但没有工作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 

我的错误在哪里?

回答

3

您正在将BeaconsBroadcast声明为BroadcastReceiver,但正在使用startService(i)调用导致该问题的原因。
您需要使用sendBroadcast(i)函数向BroadcastReceiver发送广播。 此外,由于你设置的android:出口= “假” 的广播接收器使用

LocalBroadcastManager.getInstance(上下文的背景下).sendBroadcast(意向)

+0

你好,我使用this.sendBroadcast(i);没有标记错误,但没有工作:/ mi class BeaconsBroacast不执行:/ – Luis 2014-12-02 17:39:39

+0

使用LocalBroadcastManager.getInstance(this).sendBroadcast(i),因为您已将其定义为android:exported =“false”,因此只需要本地广播。 – 2014-12-02 17:42:47

+0

我有@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

0

您好IM舱单

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

罚款应用程序标记