Android——使用通知

    通知的用法还是比较灵活的,既可以在活动里创建,也可以在广播接收器中创建,也可以在服务中创建,一般在活动中创建通知的场景还是比较少的,因为一般只有当程序进入到后台的时候我们才需要使用通知。

    无论在哪里创建通知,步骤都是相同的。首先我们需要一个NotificationManager来对通知进行管理,可以调用Context的getSystemService()方法获取的,这个方法接收一个字符串参数用于确定获取系统的哪个服务,我们传入Context.NOTIFICATION_SERVICE即可

//eg:
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
接下来使用一个Builder构造器来创建Notification对象,但API不稳定性再通知上凸显的尤为严重,所以,我们使用suport中提供的兼容API。support-v4库中提供了一个NotificationCompat类,使用这个类的构造器来创建Notification对象,就可以保证我们的床许在所有Android系统版本上都能正常工作
Notification notification = new NotificationCompat.Builder(context).build();
上述代码创建了一个空的Notification对象,我们可以在最终的build()方法之前连追任意多的设置方法来创建一个丰富的Notification对象,来看一些基本设置
Notification notification = new NotificationCompat.Builder(context)
                        .setContentTitle("this is context title")
                        .setContentText("this is context text")
                        .setWhen(System.currentTimeMillis())//指定通知被创建的时间,以毫秒为单位,当下啦系统通知栏是,这里指定的时间会显示在相应的通知上
                        .setSmallIcon(R.drawable.small_icon)//用于设置通知的小图标
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.large_icon))
                        .build();

setSmallIcon()只能使用纯alpha涂层的图片进行设置,小图标会显示在系统状态栏上。

setLargeIcon()用于设置通知大图标,当下拉系统状态栏时,就可以看见设置的大图标了。

以上工作都做完之后,只要调用NotificationManager的notify()方法就可以让通知显示出来了。notify()方法接收两个参数,第一个参数时id,要保证每个通知所指定的id,为保证每个用之所指定的id都是不同的。第二个参数则是Notification对象,这里直接将我们刚刚创建好的Notification对象传入即可。

显示一个通知可以写成

manager.notifi(1,notification);

下面我们完成一个点击事件通知

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button sendNotice = findViewById(R.id.send_notice);
        sendNotice.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.send_notice:
                NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
                Notification notification = new NotificationCompat.Builder(this)
                        .setContentTitle("这是一个通知")
                        .setContentText("这是通知中的文字")
                        .setWhen(System.currentTimeMillis())
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                        .build();
                manager.notify(1,notification);
                break;
                default:
                    break;
        }
    }
}

一般来说我们点击通知都会进入应用,我们需要在电脑中进行设置,我们可以使用PendingIntent,它和Intent有一些类似,他们之间存在共同点,都可以去指明一个意图。Intent倾向于立即执行某个动作,而PendingIntent更倾向于在某个合适的时机去执行某个动作。可以吧PendingIntent理解为延迟执行的Intent。

PendingIntent中主要提供了几个静态方法用于获取PendingIntent的实例,可以根据需求来选择是使用getActiviy()、getBroadcast()、getService()方法,他们所接受的参数都是相同的。

1、Context

2、一般用不到,通常传入0

3、是一个Intent对象,我们可以通过这个对象构建出PendingIntent的“意图”

4、用于确定PendingIntent的行为:

有FLAG_ONE_SHOT、FLAG_NO_CREATE、FLAG_ CANCEL_CURRENT、 FLAG_UPDATE_CURRENT这四种值可以选,通常传入0

我们再回来看NotificationCompat.Builder,这个构造器还可以连缀一个setContentIntent()方法,接受的参数是PendingIntent。

现在我们准备好另一个活动,新建NotificationActivity

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<TextView
    android:text="你进入了应用"
    android:textSize="40dp"
     android:layout_centerInParent="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</RelativeLayout>
public void onClick(View v) {
        switch (v.getId()){
            case R.id.send_notice:
                Intent intent = new Intent(this,NotificationActivity.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
                NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
                Notification notification = new NotificationCompat.Builder(this)
                        .setContentTitle("这是一个通知")
                        .setContentText("这是通知中的文字")
                        .setWhen(System.currentTimeMillis())
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                        .setContentIntent(pendingIntent)//添加
                        .build();
                manager.notify(1,notification);
                break;
                default:
                    break;
        }
    }



猜你喜欢

转载自blog.csdn.net/castanea/article/details/80429856