1
我使用如何使用廣播
String code = intent.getStringExtra("CODE");
我得到空指針異常獲取額外的數據時,廣播監聽器類的onReceive
方法使用
Intent intent = new Intent();
intent.setAction("com.example.android.name");
intent.putExtra("CODE", code);
發送廣播發送數據。 任何幫助如何檢索數據。
public class ReceiveNetworkBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
String code = extras.getString("CODE");
Log.e("NET_BCAST_RECEIVER: ", code);
}
}
<receiver
android:name=".ReceiveNetworkBroadcast"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.android.name" />
</intent-filter>
</receiver>
public void sendBroadcast(Context context, String code){
Intent intent = new Intent();
intent.setAction("com.example.android.name");
intent.putExtra("CODE", code);
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
context.sendBroadcast(intent);
}
可能重複[什麼是空指針異常,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how -do-i-fix-it) –
是代碼變量類型的字符串嗎? – nurisezgin
@nurisezgin是的,代碼被定義爲字符串 – Akzwitch