上一節是通知的簡單入門,在這節將介紹通知播放聲音,手機振動,點擊通知跳轉頁面的功能。
先貼出佈局代碼。
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;
}
}
}