Notification用法复习(3.0以后版本)

使用Notification的示例代码:

public class Main extends Activity implements OnClickListener {

private NotificationManager notificationManager;
private Notification.Builder mBuilder;
private Notification mNotification;

private void showNotification(String tickerText, String contentTitle,
String contentText, int smallIconId, int bigIconId) {
// 如果同类型消息还显示在通知栏,则仅更新消息条数.
if (mBuilder != null && mNotification != null) {
RemoteViews contentView = mNotification.contentView;
contentView.setTextViewText(R.id.fileName, contentText);
mBuilder.setContentTitle(contentTitle);
mBuilder.setTicker(tickerText);
mBuilder.setWhen(System.currentTimeMillis());
// mBuilder.setContentInfo("提示消息");
// mBuilder.setContentText(contentText);
notificationManager.notify(1, mBuilder.getNotification());
return;
}

Intent notificationIntent = new Intent(this, Main.class);
// 如果当前Activity启动在前台,则不开启新的Activity。
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);

mBuilder = new Notification.Builder(this);

// 设置下拉列表里的标题
mBuilder.setContentTitle(contentTitle);

// mBuilder.setContentInfo("提示消息");
// mBuilder.setContentText(contentText);

// 自定义显示消息内容
RemoteViews contentView = new RemoteViews(getPackageName(),
R.layout.inbox);
contentView.setTextViewText(R.id.fileName, contentText);
// 指定个性化视图
mBuilder.setContent(contentView);

mBuilder.setContentIntent(contentIntent);

// mBuilder.setDeleteIntent(contentIntent);

// 设置状态栏里面的图标(小图标)  
mBuilder.setSmallIcon(smallIconId);

// 下拉下拉列表里面的图标(大图标)
mBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(),
R.drawable.smile));

// 状态栏显示的标题文本.
mBuilder.setTicker(tickerText);

mBuilder.setDefaults(Notification.DEFAULT_SOUND);
// 设置事件发生时间
mBuilder.setWhen(System.currentTimeMillis());
// Setting this flag will make it so the notification is automatically
// canceled when the user clicks it in the panel.
mBuilder.setAutoCancel(true);

mNotification = mBuilder.getNotification();
notificationManager.notify(1, mNotification);
}

int i = 0;
int j = 0;

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSmile:
showNotification("您收到的消息数:" + (++i) + "条!", "消息数", "您收到的消息数:"
+ (++j) + "条!", R.drawable.why, R.drawable.smile);
break;
}
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 实例化NotificationManager.
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Button btnSmile = (Button) findViewById(R.id.btnSmile);
btnSmile.setOnClickListener(this);
}

@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
System.out.println("onNewIntent() method called!!!");
i = 0;
j = 0;
}
}


有一个点我们需要注意就是如果要在点击Notification栏的消息,打开本来已经启动的Activity时,我们需要在AndroidMenifest.xml配置文件中设置Activity的启动方式:
<activity
android:name=".Main"
android:label="@string/app_name"
android:launchMode="singleTop" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值