廣播接收器、
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ex37.BroadcastReceiver_2"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="14" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="ex37.BroadcastReceiver_2.BroadcastReceiver_2"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="ex37.BroadcastReceiver_2.TargetActivity"/>
<receiver android:name="ex37.BroadcastReceiver_2.ToastBroadcastReceiver">
<intent-filter>
<action android:name="android.broadcast.TOAST"/>
</intent-filter>
</receiver>
<receiver android:name="ex37.BroadcastReceiver_2.NotificationBroadcastReceiver">
<intent-filter>
<action android:name="android.broadcast.NOTIFICATION"/>
</intent-filter>
</receiver>
<receiver android:name="ex37.BroadcastReceiver_2.ActivityBroadcastReceiver">
<intent-filter>
<action android:name="android.broadcast.ACTIVITY"/>
</intent-filter>
</receiver>
</application>
</manifest>
bROADCASTrECEIVER_2
package ex37.BroadcastReceiver_2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class BroadcastReceiver_2 extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
// Button元件的事件處理
public void btn1_Click(View view)
{
Intent i = new Intent("android.broadcast.TOAST");
sendBroadcast(i);
}
public void btn2_Click(View view)
{
Intent i = new Intent("android.broadcast.NOTIFICATION");
sendBroadcast(i);
}
public void btn3_Click(View view)
{
Intent i = new Intent("android.broadcast.ACTIVITY");
sendBroadcast(i);
}
}
ActivityBroadcastReceiver
package ex37.BroadcastReceiver_2;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class ActivityBroadcastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Intent i = new Intent(context, TargetActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
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 NotificationBroadcastReceiver extends BroadcastReceiver
{
private static final int NOTIF_ID = 1;
@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent)
{
NotificationManager notifMgr = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification note = new Notification(R.drawable.icon,
"收到通知廣播!", System.currentTimeMillis());
Intent i = new Intent(context, BroadcastReceiver_2.class);
PendingIntent pi = PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
note.setLatestEventInfo(context, "這是廣播送出的通知", null, pi);
notifMgr.notify(NOTIF_ID, note);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ex37.BroadcastReceiver_2"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="14" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="ex37.BroadcastReceiver_2.BroadcastReceiver_2"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="ex37.BroadcastReceiver_2.TargetActivity"/>
<receiver android:name="ex37.BroadcastReceiver_2.ToastBroadcastReceiver">
<intent-filter>
<action android:name="android.broadcast.TOAST"/>
</intent-filter>
</receiver>
<receiver android:name="ex37.BroadcastReceiver_2.NotificationBroadcastReceiver">
<intent-filter>
<action android:name="android.broadcast.NOTIFICATION"/>
</intent-filter>
</receiver>
<receiver android:name="ex37.BroadcastReceiver_2.ActivityBroadcastReceiver">
<intent-filter>
<action android:name="android.broadcast.ACTIVITY"/>
</intent-filter>
</receiver>
</application>
</manifest>
bROADCASTrECEIVER_2
package ex37.BroadcastReceiver_2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class BroadcastReceiver_2 extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
// Button元件的事件處理
public void btn1_Click(View view)
{
Intent i = new Intent("android.broadcast.TOAST");
sendBroadcast(i);
}
public void btn2_Click(View view)
{
Intent i = new Intent("android.broadcast.NOTIFICATION");
sendBroadcast(i);
}
public void btn3_Click(View view)
{
Intent i = new Intent("android.broadcast.ACTIVITY");
sendBroadcast(i);
}
}
ActivityBroadcastReceiver
package ex37.BroadcastReceiver_2;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class ActivityBroadcastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Intent i = new Intent(context, TargetActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
NotificationBroadcastReceiver
package ex37.BroadcastReceiver_2;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 NotificationBroadcastReceiver extends BroadcastReceiver
{
private static final int NOTIF_ID = 1;
@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent)
{
NotificationManager notifMgr = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification note = new Notification(R.drawable.icon,
"收到通知廣播!", System.currentTimeMillis());
Intent i = new Intent(context, BroadcastReceiver_2.class);
PendingIntent pi = PendingIntent.getActivity(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
note.setLatestEventInfo(context, "這是廣播送出的通知", null, pi);
notifMgr.notify(NOTIF_ID, note);
}
}
TargetActivity
package ex37.BroadcastReceiver_2;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
public class TargetActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
new AlertDialog.Builder(this)
.setMessage("收到活動廣播!")
.show();
}
}
ToastBroadcastReceiver
package ex37.BroadcastReceiver_2;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class ToastBroadcastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "收到Toast廣播!",
Toast.LENGTH_SHORT).show();
}
}
留言
張貼留言