广播发送
String actionString = "com.androidbook.intens.testbc"; Intent broadcastIntent = new Intent(actionString); broadcastIntent.putExtras("message","Hello World"); this.sendBroadcast(broadcastIntent);
广播接收
1 private static final String tag = "TestReceiver"; 2 3 public onReceive(Context context, Intent intent) 4 { 5 6 Utils.logThreadSignature(tag); 7 Log.d("TestReceiver","intent="+intent); 8 String message = intent.getStringExtras("message"); 9 Log.d(tag,mesage); 10 }
Utils类记录运行接收程序代码的线程的签名。
public class Utils { public static long getThreadId() { Thread t = Thread.currentThread(); return t.getId(); } public static String getThreadSignature() { Thread t = Thread.currentThread(); long l = t.getId(); String name = t.getName(); long p = t.getPriority(); String gname = t.getThreadGroup().getName(); return (name+“id”+1+"priority"+p+":group"+gname); } public static void logThreadSignature(String tag) { Log.d(tag, getThreadSignature()); } }