|
启动AcStart,负责在创建时立即创建一个Notification在状态栏中,处理动作为后面的AcNotificationDealer。
AcStart.java
package jtapp.notifications;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
public class AcStart extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(ns);
Long when = System.currentTimeMillis();
CharSequence tickerText = "My notification: Hello World! Click to deal this notification.";
Notification notification =
new Notification(R.drawable.icon, tickerText, when);
notification.vibrate = new long[] {100,250,100,500};
Context context = getApplicationContext();
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World! Click to deal this notification.";
Intent notificationIntent =
new Intent(this, AcNotificationDealer.class);
PendingIntent contentIntent =
PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(
context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(9999, notification);
}
}
取消上一通知,创建10s定时器通知。
AcNotificationDealer.java
package jtapp.notifications;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class AcNotificationDealer extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notifdealer);
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(ns);
mNotificationManager.cancel(9999);
Intent intent = new Intent(
AcNotificationDealer.this, ReceiverAlarm.class);
PendingIntent appIntent =
PendingIntent.getBroadcast(
AcNotificationDealer.this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), appIntent);
Toast.makeText(this, "10秒后在状态栏将创建定时器通知.", Toast.LENGTH_LONG).show();
}
}
接受10s定时器通知,并创建状态栏通知。
ReceiverAlarm.java
package jtapp.notifications;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
public class ReceiverAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(ns);
Long when = System.currentTimeMillis();
CharSequence tickerText = "My Alarm Fired:Hello World Alarm! Click ready to dial 10010.";
Notification notification =
new Notification(R.drawable.icon, tickerText, when);
notification.vibrate = new long[] {100,250,100,500};
CharSequence contentTitle = "My Alarm Fired";
CharSequence contentText = "Hello World Alarm! Click ready to dial 10010.";
Intent notificationIntent =
new Intent(Intent.ACTION_DIAL, Uri.parse("tel:10010"));
PendingIntent contentIntent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(
context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(9998, notification);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
androidrientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="启动该Activity后, 会在状态栏产生一个通知,该通知用来进入通知处理Activity."
/>
</LinearLayout>
notifdealer.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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="此页面为通知处理页面,用来处理点击状态栏Notification后的流程,状态栏上会自动取消该通知,并且设定10秒后创建Dial 10010的定时器通知。"
/>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="jtapp.notifications"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AcStart"
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="AcNotificationDealer"></activity>
<receiver android:name="ReceiverAlarm"></receiver>
</application>
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
</manifest> |
|