通知的应用

来源:互联网 发布:嘀哩嘀哩软件下载 编辑:程序博客网 时间:2024/05/05 11:31

通知可以在活动,广播,服务中创建

简单通知的应用:

package com.example.notificationtest;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.provider.Settings;import android.support.v4.app.NotificationManagerCompat;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends Activity implements View.OnClickListener {    private Button sendNotice;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        sendNotice = (Button) findViewById(R.id.send_notice);        sendNotice.setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()){            case R.id.send_notice:                Notification.Builder builder = new Notification.Builder(MainActivity.this);                builder.setTicker("This is a trcker");//通知栏的预览文字                builder.setSmallIcon(R.mipmap.ic_launcher);//图标                builder.setContentTitle("This is title");//通知的标题                builder.setContentText("This is content");//通知的内容                builder.setWhen(System.currentTimeMillis());//设置通知时间为当前系统时间                builder.setVibrate(new long[] {0, 1000, 1000, 1000});//为通知设置震动,4个时间参数,单位为毫秒。                    //第1个参数是延迟振动时间,第2个是振动时间,第3个是静止时间,第4个是静止后又振动的时间                    //上边这个振动就是收到通知后,手机立刻振动1秒,停止振动1秒,接着再振动1秒。               // Uri soundUri = Uri.fromFile("路径");               // builder.setSound(soundUri);//设置指定路径下的通知提示音                builder.setDefaults(Notification.DEFAULT_ALL);//通知的提示音铃声振动全部采用系统默认值。                        //也可以单独设定Notification.DEFAULT_LIGHTS或Notification.DEFAULT_SOUND或Notification.DEFAULT_VIBRATE                        //设置默认后,自定义的铃声和振动都无效。                Notification notification = builder.build();//获取Notification的实例                NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);                manager.notify(1, notification);                //设置唯一的通知Id        }    }}

加入点击事件:通知的应用二

package com.example.notificationtest;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.provider.Settings;import android.support.v4.app.NotificationManagerCompat;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends Activity implements View.OnClickListener {    private Button sendNotice;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        sendNotice = (Button) findViewById(R.id.send_notice);        sendNotice.setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()){            case R.id.send_notice:                //设置点击事件                Intent intent = new Intent(MainActivity.this, NotificationActivity.class);//点击通知后启动NoticeActivity                PendingIntent pi = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);//                在使用中,碰到了一些问题,主要是参数FLAG_CANCEL_CURRENT和FLAG_UPDATE_CURRENT,总结如下:////                PendingIntent有一个getActivity方法,第一个参数是上下文,没啥好说的,//第二个参数 requestCode,这个后面说,//第三个参数是 Intent,用来存储信息,//第四个参数是对参数的操作标识,常用的就是FLAG_CANCEL_CURRENT和FLAG_UPDATE_CURRENT。////                当使用FLAG_UPDATE_CURRENT时:////                PendingIntent.getActivity(context, 0, notificationIntent,PendingIntent.FLAG_CANCEL_CURRENT时);//                FLAG_UPDATE_CURRENT会更新之前PendingIntent的消息,比如,你推送了消息1,并在其中的Intent中putExtra了一个值“ABC”,//在未点击该消息前,继续推送第二条消息,并在其中的Intent中putExtra了一个值“CBA”,//好了,这时候,如果你单击消息1或者消息2,你会发现,//他俩个的Intent中读取过来的信息都是“CBA”,就是说,第二个替换了第一个的内容////                当使用FLAG_CANCEL_CURRENT时://                依然是上面的操作步骤,这时候会发现,点击消息1时,没反应,第二条可以点击。                Notification.Builder builder = new Notification.Builder(MainActivity.this);                builder.setTicker("This is a trcker");//通知栏的预览文字                builder.setSmallIcon(R.mipmap.ic_launcher);//图标                builder.setContentTitle("This is title");//通知的标题                builder.setContentText("This is content");//通知的内容                builder.setWhen(System.currentTimeMillis());//设置通知时间为当前系统时间                builder.setVibrate(new long[] {0, 1000, 1000, 1000});//为通知设置震动,4个时间参数,单位为毫秒。                    //第1个参数是延迟振动时间,第2个是振动时间,第3个是静止时间,第4个是静止后又振动的时间                    //上边这个振动就是收到通知后,手机立刻振动1秒,停止振动1秒,接着再振动1秒。               // Uri soundUri = Uri.fromFile("路径");               // builder.setSound(soundUri);//设置指定路径下的通知提示音                builder.setDefaults(Notification.DEFAULT_ALL);//通知的提示音铃声振动全部采用系统默认值。                        //也可以单独设定Notification.DEFAULT_LIGHTS或Notification.DEFAULT_SOUND或Notification.DEFAULT_VIBRATE                        //设置默认后,自定义的铃声和振动都无效。                builder.setContentIntent(pi);                Notification notification = builder.build();//获取Notification的实例                NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);                manager.notify(1, notification);                break;            default:                    break;        }    }}
package com.example.notificationtest;import android.app.Activity;import android.app.NotificationManager;import android.os.Bundle;import android.os.PersistableBundle;/** * Created by yuwei on 2016/9/25. */public class NotificationActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.notification_layout);        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);        manager.cancel(1);//设定的通知Id    }}

拓展:三者的区别
Notification:
A class that represents how a persistent notification is to be presented to the user using the NotificationManager.
The Notification.Builder has been added to make it easier to construct Notifications.
*构建Notifications主要用的类,发现好多方法都给移除给NotificationCompat.Builder替代了

Notification.Builder:
Builder class for Notification objects. Provides a convenient way to set the various fields of a Notification and generate content views using the platform’s notification layout template. If your app supports versions of Android as old as API level 4, you can instead use NotificationCompat.Builder, available in the Android Support library.
*Notification.Builder是为了让开发者更容易构建出Notifications而诞生的。

NotificationCompat.Builder
Builder class for NotificationCompat objects. Allows easier control over all the flags, as well as help constructing the typical notification layouts.
*NotificationCompat.Builder,由上面的加粗可以看出,NotificationCompat.Builder是解决Notification.Builder的兼容问题而诞生的。compat:兼容性

把这三个搞清楚之后,我直接用Notificatio

0 0