android notification的支持

做軟件這麼多年,感覺中國和國外資料的巨大差異,決定在文檔中多用英文,以方便自己看英文資料

APendingIntentis simply a holder for an intent and target action.

android的通知有4個可選參數:

JAn icon: to display in the status bar.
JOptional ticker text: that will be displayed in the status bar when the notifiation is fist shown.
JThe title and message :to display in the notifiation tray. This is alsooptional.

JA requiredPendingIntentto trigger when the user taps the notifiation.


Here is an example notifiation:
int icon = R.drawable.icon;
CharSequence ticker = “Hello World!”;
long now = System.currentTimeMillis();
Notification notification = new Notification(icon, ticker, now);


Once you have created a notifiation, use theNotificationManagerto display
that notifiation to the user:

NotificationManager nm = (NotificationManager)
pgetSystemService(NOTIFICATION_SERVICE);
Context context = getApplicationContext();
CharSequence message = “Hello World!”;
Intent intent = new Intent(this, Example.class);
String title = “Hello World!”;
String message = “This is a message.”;
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
pintent, 0);
notification.setLatestEventInfo(context, title, message,
ppendingIntent);
nm.notify(ID, notification);

notification tips:

You can do more than just update the status bar with notifiations—
you have the option of playing a sound, flshing an LED, or vibrating
the device when the notifiation is triggered. You do this by using the
notification.defaultsoption. For example, to play the default notifiation
sound, set the defaults option to:
notification.defaults |= Notification.DEFAULT_SOUND;
This will play the user-confiured notifiation sound when your notifiation
is displayed. You can also use a custom sound for the sound option:
notification.sound = Uri.parse(“file:///sdcard/mysound.mp3”);
There are custom options for LED flshing and vibrations as well. check out the
Notificationclass in the Android documentation for all options available. Keep
in mind that not all devices will have LED indicators or vibration capability.


tip:android 3.0 (honeycomb) introduced theNotification.
Builder class for creating notifications. this builder replaces the
existing constructor for the Notification class and makes creating notifi-
cations easier. the notification id for each notification should be globally
unique in your app. You should list all these ids in a common file to ensure
their uniqueness, or strange behavior may result.


package com.example.tonyjiang81.notificatin;


import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.util.Log;
import android.view.View;
import android.widget.Button;


public class MainActivity extends Activity {
private NotificationManager mNotifyManager;
private Builder mBuilder;
int id = 1;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {


public void onClick(View arg0) {
mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(MainActivity.this);
mBuilder.setContentTitle("Download")
.setContentText("Download in progress")
.setSmallIcon(R.drawable.ic_download);



new Downloader().execute();
}
});
}


private class Downloader extends AsyncTask<Void, Integer, Integer> {


@Override
protected void onPreExecute() {
super.onPreExecute();


// Displays the progress bar for the first time.
mBuilder.setProgress(100, 0, false);
mNotifyManager.notify(id, mBuilder.build());

}


@Override
protected void onProgressUpdate(Integer... values) {
// Update progress
mBuilder.setProgress(100, values[0], false);
mNotifyManager.notify(id, mBuilder.build());
super.onProgressUpdate(values);
}


@Override
protected Integer doInBackground(Void... params) {
int i;
for (i = 0; i <= 100; i += 5) {
// Sets the progress indicator completion percentage
publishProgress(Math.min(i, 100));
try {
// Sleep for 5 seconds
Thread.sleep(2 * 1000);
} catch (InterruptedException e) {
Log.d("TAG", "sleep failure");
}
}
return null;
}


@Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
mBuilder.setContentText("Download complete");
// Removes the progress bar
mBuilder.setProgress(0, 0, false);
mNotifyManager.notify(id, mBuilder.build());
}
}
}



發佈了52 篇原創文章 · 獲贊 10 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章