您的位置:首页 > 编程语言

《第一行代码》第8章 --通知的使用修正

2016-06-05 21:24 176 查看
《第一行代码》第8章的中涉及的通知的基本用法处,由于Notificition已被弃用,所以代码出错。需要更新写法。下面列出的是可用的修正写法。同时,第九章P365页处用到的通知,也需要重新写。

1、P300 页源码及修正



@Override
private void onClick(View v){
switch (v.getId()){
case R.id.send_notice:
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.s3,"this is ticker text",
System.currentTimeMillis());
notification.setLatestEventInfo(this,"this is title","this is text",null);
manager.notify(1,notification);
break;
default:
break;
}
}


更新为



@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.send_notice:
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.s3);
builder.setTicker("just watch me");
builder.setWhen(System.currentTimeMillis());
builder.setContentTitle("look at that");
builder.setContentText("you can do it!");
Intent intent = new Intent(this,NotificationActivity.class);
PendingIntent pi = PendingIntent.getActivity(this,0,intent,
PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(pi);

//Uri soundUri = Uri.fromFile(new File("/system/media/audio/ringtones/Basic_tone.ogg"));
//builder.setSound(soundUri);

//long[] vibrates = {0,1000,1000,1000};
//builder.setVibrate(vibrates);

//builder.setLights(Color.GREEN,1000,1000);
builder.setDefaults(Notification.DEFAULT_ALL);
manager.notify(1, builder.build());
break;
default:
break;


2、P365 页源码及修正



Notification notification = new Notification(R.drawable.a10,"Notification comes",
System.currentTimeMillis());
Intent notificationIntent = new Intent(this,MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,notificationIntent,0);
notification.setLatestEventInfo(this,"this is title","this is content",pendingIntent);
startForeground(1,notification);
Log.d("MyService","onCreate executed");


更新为:



NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setTicker("just watch me");
builder.setWhen(System.currentTimeMillis());
builder.setContentTitle("look at that");
builder.setContentText("you can do it!");
startForeground(1,builder.build());
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: