<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rainsong.broadcastdemo"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<activity android:name="MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="MyReceiver">
<intent-filter>
<action android:name="com.rainsong.action.NEW_BROADCAST" />
</intent-filter>
</receiver>
</application>
</manifest>布局文件:main.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="发送广播"
/>
</LinearLayout>Java源代码文件:MainActivity.javapackage com.rainsong.broadcastdemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity
{
public final String ACTION = "com.rainsong.action.NEW_BROADCAST";
Button btn1;
OnClickListener listener1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button)findViewById(R.id.button1);
listener1 = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(ACTION);
sendBroadcast(intent);
}
};
btn1.setOnClickListener(listener1);
}
}Java源代码文件:MyReceiver.javapackage com.rainsong.broadcastdemo;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyReceiver extends BroadcastReceiver {
public static int NOTIFICATION_ID = 12345;
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager nm =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(android.R.drawable.stat_notify_chat,
"MyReceiver onReceive", System.currentTimeMillis());
Intent intent1 = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent1, 0);
notification.setLatestEventInfo(context, "MyReceiver onReceive", null, pendingIntent);
nm.notify(NOTIFICATION_ID, notification);
}
}原文:http://blog.csdn.net/hantangsongming/article/details/41448415