Android「1行目のコード」学習ノート---古い通知の書き方


8.29更新—>通知に関する記事http://blog.csdn.net/vipzjyno1/article/details/25248021今日、最初の行のコード365ページのサービス通知の書き方を読みました.中にはもう時代遅れでない方法がたくさんあることに気づいた.
ブックコード
:
Notification notification = new Notification(R.drawable.ic_launcher, "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);  

ここで、Notificationのインスタンス化は時代遅れであり、setLatestEventInfoの方法はない.
調査資料によると、Notification.Builder builderを導入してNotificationを実例化する予定だった.
新しいコード
:具体的には以下の通りです.
Intent notificationIntent=new Intent(this,MainActivity.class);
        PendingIntent pendingIntent=PendingIntent.getActivity(this,0,notificationIntent,0);
        Notification.Builder builder = new Notification.Builder(this);
        Notification notification=builder.setContentIntent(pendingIntent)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setWhen(System.currentTimeMillis()).setAutoCancel(false)
                .setContentTitle("statusContentTitle").setContentText("statusContentContext").build();
startForeground(1,notification);

Builder setの各種設定を使用する、最後に.buid().
テスト:Android《第一行代码》学习笔记---过时的通知写法_第1张图片
補足
builder.getNotification()も時代遅れの方法なので、もう使わないほうがいいです.
今日は300ページの通知の書き方をめくったが、やはり時代遅れだった.
ブックコード:
    Notification notification = new Notification(R.drawable.strawberry,"this is ticker text",System.currentTimeMillis());  
    notification.setLatestEvenInfo(this,"This is content title","This is context text",null);
    manager.notify(1,notofication);

コードの変更:
//          ,     、       。
 NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle("    ")//       
                        .setContentText("    ") //         
               // .setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL)) //         
                    //  .setNumber(number) //         
                    .setTicker("      ") //          ,        
                    .setWhen(System.currentTimeMillis())//       ,         ,           
                    .setPriority(Notification.PRIORITY_DEFAULT) //        
                    //  .setAutoCancel(true)//                        
                    .setOngoing(false)//ture,             。               ,      (     )          ,      (       ,    ,      )
                    .setDefaults(Notification.DEFAULT_VIBRATE)//       、           、                  ,  defaults  ,    
                    //Notification.DEFAULT_ALL  Notification.DEFAULT_SOUND      // requires VIBRATE permission
                    .setSmallIcon(R.mipmap.ic_launcher);//     ICON
Notification notification = mBuilder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(1, mBuilder.build());

Android《第一行代码》学习笔记---过时的通知写法_第2张图片