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());
}
}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值