在被意图调用时未收到广播

时间:2014-11-17 05:10:32

标签: android broadcastreceiver

我有广播,它是在系统启动时以及通过意图收到的。这个广播是设置闹钟。

Intent intent = new Intent();
intent.setAction("recievers.BroadCastBootRec");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
getActivity().sendBroadcast(intent);

我的广播课程:

@Override
public void onReceive(Context context, Intent intent) {
    c = context;
    Log.d("HirakDebug", "BroadCast Recieved");
    getDatesFromDatabase();
    getDateDifference();
    setAlarmI();
}

的Manifest.xml

<receiver android:name=".recievers.BroadCastBootRec"
        android:label="BootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</receiver>

系统重新启动,但不是由意图获得。

3 个答案:

答案 0 :(得分:1)

你必须注册两个动作 并使事情清楚。 android:name是你编写的接收者的类名,而不是动作。 因此,对于下面的接收器,您的接收器类名称将为“BroadCastBootRec”

<receiver android:name=".recievers.BroadCastBootRec"
    android:label="BootReceiver">
<intent-filter>
    <action android:name="com.example.BroadCastBootRec" />
    <action android:name="android.intent.action.BOOT_COMPLETED" />
    <category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>


Intent intent = new Intent();
intent.setAction("com.example.BroadCastBootRec");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
getActivity().sendBroadcast(intent);

当系统 BOOT_COMPLETED 并且 com.example.BroadCastBootRec 被触发时,Android系统会通知您的接收方

public class BroadCastBootRec extends BroadcastReceiver 
  @Override
  public void onReceive(Context context, Intent intent) {
   c = context;
   Log.d("HirakDebug", "BroadCast Recieved");
   getDatesFromDatabase();
   getDateDifference();
   setAlarmI();
}
}

答案 1 :(得分:1)

请在意图过滤器上添加自定义操作:

<action android:name="recievers.BroadCastBootRec" />

示例:

<receiver android:name=".recievers.BroadCastBootRec"
   android:label="BootReceiver">
   <intent-filter>
      <action android:name="android.intent.action.BOOT_COMPLETED" />
      <action android:name="recievers.BroadCastBootRec" />
      <category android:name="android.intent.category.DEFAULT"/>
   </intent-filter>
</receiver>

答案 2 :(得分:0)

代码应该是

MainActivity.java

public void broadcastCustomIntent(View view)
{
   Intent intent = new Intent("MyCustomIntent");

   EditText et = (EditText)findViewById(R.id.extraIntent);
   // add data to the Intent
   intent.putExtra("message", (CharSequence)et.getText().toString());
   intent.setAction("com.javacodegeeks.android.A_CUSTOM_INTENT");
   sendBroadcast(intent);
}

在MyBroadcastReceiver.java中

public class MyBroadcastReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    // Extract data included in the Intent
    CharSequence intentData = intent.getCharSequenceExtra("message");   
    Toast.makeText(context, "Javacodegeeks received the Intent's message: "+intentData,    Toast.LENGTH_LONG).show();
}

}

在app的清单文件中

  <receiver android:name="MyBroadcastReceiver">
        <intent-filter>
            <action android:name="com.javacodegeeks.android.A_CUSTOM_INTENT">
            </action>
        </intent-filter>
  </receiver>