上一节是通知的简单入门,在这节将介绍通知播放声音,手机振动,点击通知跳转页面的功能。
先贴出布局代码。
activity_main.xml 这是触发界面的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.webview.MainActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/web_view"
/>
</RelativeLayout>
notification_layout.xml 这是跳转之后页面的布局,都很简单
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview"
android:text="这是通知跳转后的页面"
/>
</LinearLayout>
NotificationActivity.java
package com.example.notification;
import android.app.Activity;
import android.os.Bundle;
/**
* Created by yzc on 2016/12/7.
*/
public class NotificationActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification_layout);
}
}
MainActivity.java
package com.example.notification;
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.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.io.File;
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 view) {
switch (view.getId()){
case R.id.send_notice:
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);//创建一个NotificationManager对通知进行管理
Notification.Builder builder = new Notification.Builder(this);
builder.setContentInfo("补充内容");
builder.setContentText("主内容区");
builder.setContentTitle("通知标题");
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setTicker("新消息");
builder.setAutoCancel(true);//跳转页面后通知是否消失,true存在false不消失。默认是false。
builder.setWhen(System.currentTimeMillis());
//提示音
Uri soundUri = Uri.fromFile(new File("/system/media/audio/ringtones/MI.ogg"));
builder.setSound(soundUri);
//振动
long[] vibrates = {0,1000,1000,1000};
builder.setVibrate(vibrates);
// //呼吸灯
// builder.setLights(Color.BLUE,1000,1000);
// //呼吸灯没有验证成功因为在来电或者系统屏幕亮的情况下是没办法控制灯的,具体见具体源码(com.android.server.NotificationManagerService)
// //等学习了延时操作再试试
//跳转操作
Intent intent = new Intent(this, NotificationActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(pendingIntent);
Notification notification = builder.build();
manager.notify(1, notification);//第一个参数设置id
break;
default:
break;
}
}
}