嗨朋友们,我无法将广播从一项活动发送到其他活动,请参阅下面的代码并提供帮助:
public class SendBroadcast extends Activity {
public static String BROADCAST_ACTION = "com.unitedcoders.android.broadcasttest.SHOWTOAST";
/* }
});
}
public void sendBroadcast(){
Intent broadcast = new Intent("com.unitedcoders.android.broadcasttest.SHOWTOAST");
this.sendBroadcast(broadcast);
//startActivity(broadcast);
}
}
接收方代码:
public class ToastDisplay extends Activity {
private BroadcastReceiver mReceiver;
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.i("!!!!!!!InchooTutorial@@@@@@@$$$$","%%%%%%% msg_for_me");
ent // String msg_for_me = intent.getStringExtra("some_msg"); //记录我们的消息值 Log.i("!!!!!!!InchooTutorial@@@@@@@$$$$","%%%%%%% msg_for_me");
}
};
//registering our receiver
this.registerReceiver(mReceiver, intentFilter);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
//unregister our receiver
this.unregisterReceiver(this.mReceiver);
}
}
Manifest.xml 是:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.unitedcoders.android.broadcasttest"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.BROADCAST_STICKY"/>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SendBroadcast"
android:label="@string/app_name">
<intent-filter>
nitedcoders.android.broadcasttest.SHOWTOAST" />
</application> </manifest>