什麼是通知:
當某個程式希望向使用者發送一些提示資訊時,而該應用又不在前台運作,這時借助通知就可以輕松實作,當觸發通知時,狀态欄會顯示一個小圖示,下拉狀态欄就可以看到通知的詳細資訊了。
執行個體講解:
效果展現:
代碼實作:(需要說明的是這個通知在Android8.0無法運作,我以後有時間會補充,我是在5.0中運作的)
1.修改activity_main.xml中的代碼,添加按鈕:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context="com.example.notificationtest.MainActivity">
<Button
android:id="@+id/send_notification_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="send notification" />
</LinearLayout>
2.修改MainActivity中的代碼,實作發送通知:
public class MainActivity extends AppCompatActivity {
private Button notifyBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notifyBtn = findViewById(R.id.send_notification_btn);
//對發送通知的按鈕進行監聽
notifyBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//建立NotificationManager對象
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//通過NotificationCompat.Builder()方法擷取Notification對象
Notification notification = new NotificationCompat.Builder(MainActivity.this)
.setContentTitle("hello world")
.setContentText("i miss you ,you know?")
.setWhen(System.currentTimeMillis())
//設定小圖示,就是點選通知按鈕後,狀态欄上的小白點
.setSmallIcon(R.mipmap.ic_launcher)
//下拉狀态欄後,看到的圖示
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.build();
//最後釋出通知
/**
* Post a notification to be shown in the status bar. If a notification with
* the same id has already been posted by your application and has not yet been canceled, it
* will be replaced by the updated information.
*
* @param id An identifier for this notification unique within your
* application. 設定ID,試你的通知唯一
* @param notification A {@link Notification} object describing what to show the user. Must not
* be null. 第二個參數就是Notification對象
*/
manager.notify(,notification);
}
});
}
}
over
但是,當我們點選這條通知時,并沒用任何反應,這和現實中不一樣,下面我們就來實作點選通知調轉到相應的頁面。
1.建立點選通知跳轉的頁面,然後需修改布局,讓你能看出實作調轉了:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.notificationtest.NotificationActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this is notification"
android:layout_gravity="center_horizontal"
android:textSize="25sp"/>
</LinearLayout>
2.修改MainActivity中不分代碼:
........
notifyBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//建立intent對象,表名intent的意圖
Intent intent = new Intent(MainActivity.this,NotificationActivity.class);
//建立PendingIntent對象,param1:目前環境,param2:通常0就行,param3:pendingIntent意圖,param4:pendingIntent意圖,一般填入0就行
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,,intent,);
//建立NotificationManager對象
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//通過NotificationCompat.Builder()方法擷取Notification對象
Notification notification = new NotificationCompat.Builder(MainActivity.this)
............
.setWhen(System.currentTimeMillis())
//點選通知後自動取消
.setAutoCancel(true)
//實作跳轉
.setContentIntent(pendingIntent)
.........
.build();
//最後釋出通知
...........
}
});
}
}
效果展現: