標籤:體驗 roi switch catch cti textview out ice 執行
一般當我們收到簡訊啊,啊,或者有些app的提醒。我們都會在通知欄收到一天簡單的訊息,然後點擊訊息進入到app裡面,事實上android中有專門的Notification的類能夠完畢這個工作,這裡就實現下這個功能。
首先建立NotificationTestproject,然後加入一個button,用來觸發通知。然後編寫代碼例如以下:
package com.example.jared.notificationtest;import android.app.NotificationManager;import android.os.Bundle;import android.support.v4.app.NotificationCompat;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity { private Button sendNotificationBtn; private int mId = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sendNotificationBtn = (Button)findViewById(R.id.sendNotification); sendNotificationBtn.setOnClickListener(new myOnClickListener()); } private class myOnClickListener implements View.OnClickListener { @Override public void onClick(View view) { switch (view.getId()) { case R.id.sendNotification: setSendNotificationBtn(); break; default: break; } } } public void setSendNotificationBtn () { NotificationCompat.Builder notification = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("My Notification") .setContentText("Hello Notification"); NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.notify(mId, notification.build()); }}
這裡了用了NotificatonCompat.Builder來建立一個簡單的Notification,setSmallIcon是指定當中的表徵圖,setContentTitle方法是指定標題,setContentText指定內容,然後通過getSystemService擷取通知的管理類,通過notify方法發送通知,當中mId是一個id號,每個通知有其獨特的通知號。不能反覆。
執行效果例如以下所看到的:
接著我們來實現點擊通知後跳轉到相應的Activity中,然後消除這條通知。再建立一個Activity,布局例如以下:
<?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" tools:context="com.example.jared.notificationtest.Notification"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="歡迎點擊通知事件!" android:layout_margin="20dp" android:textSize="20dp"/></LinearLayout>
這裡就一個textview用來顯示下資訊,接著編寫代碼例如以下:
package com.example.jared.notificationtest;import android.app.NotificationManager;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;public class Notification extends AppCompatActivity { private int mId = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_notification); NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); manager.cancel(mId); }} 這裡進入到Activity後就把通知清除掉,接著就是改動MainActivity代碼:
package com.example.jared.notificationtest;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.os.Bundle;import android.support.v4.app.NotificationCompat;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity { private Button sendNotificationBtn; private int mId = 1; private int numMessage = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sendNotificationBtn = (Button)findViewById(R.id.sendNotification); sendNotificationBtn.setOnClickListener(new myOnClickListener()); } private class myOnClickListener implements View.OnClickListener { @Override public void onClick(View view) { switch (view.getId()) { case R.id.sendNotification: setSendNotificationBtn(); break; default: break; } } } public void setSendNotificationBtn () { NotificationCompat.Builder notification = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("My Notification") .setContentText("Hello Notification") .setNumber(++numMessage); Intent intent = new Intent(this, Notification.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); notification.setContentIntent(pendingIntent); NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); manager.notify(mId, notification.build()); }}
這裡又加入了setNumber方法。主要是顯示來了幾條通知,比方中就須要知道,然後執行個體化了一個intent。再執行個體化一個pendingIntent。擷取activity,在NotificationCompat.Builder裡setContentIntent。之後就能夠達到我們的效果,執行並點擊通知例如以下所看到的:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="200" height="350" >
如上所看到的收到了6條通知,然後點擊後通知也消除了。
一般在下載歌曲啊。圖片啊的時候。會有進度條表示下載的過程,這裡來類比實現下這個功能。改動MainAcitivy代碼例如以下:
package com.example.jared.notificationtest;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.os.Bundle;import android.support.v4.app.NotificationCompat;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private Button sendNotificationBtn; private int mId = 1; private int numMessage = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sendNotificationBtn = (Button)findViewById(R.id.sendNotification); sendNotificationBtn.setOnClickListener(new myOnClickListener()); } private class myOnClickListener implements View.OnClickListener { @Override public void onClick(View view) { switch (view.getId()) { case R.id.sendNotification: setSendNotificationBtn(); break; default: break; } } } public void setSendNotificationBtn () { final NotificationCompat.Builder notification = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("Music Download") .setContentText("burning.mp3") .setNumber(++numMessage); Intent intent = new Intent(this, Notification.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); notification.setContentIntent(pendingIntent); final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); new Thread( new Runnable(){ @Override public void run() { for(int cnt=0; cnt<=100; cnt++){ notification.setProgress(100, cnt, false); manager.notify(mId, notification.build()); try { Thread.sleep(200); } catch (InterruptedException e) { Log.d(TAG, "Sleep failure"); } } notification.setContentText("Download complete"); notification.setProgress(0, 0, false); manager.notify(mId, notification.build()); } } ).start(); }}
這裡通過setProgress方法來實現,這裡開了一個Thread,當下載完畢後又一次設定下內容。
執行結果例如以下:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" width="200" height="350" >
圖1顯示運行進度條在走,圖2完畢了下載功能。
一般收到通知,手機都會有一段聲音。加上震動。那麼接下來來實現這個功能。,假設下載完畢後,就放一段音樂而且震動,改動代碼例如以下:
package com.example.jared.notificationtest;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.support.v4.app.NotificationCompat;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.view.View;import android.widget.Button;import java.io.File;public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private Button sendNotificationBtn; private int mId = 1; private int numMessage = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sendNotificationBtn = (Button)findViewById(R.id.sendNotification); sendNotificationBtn.setOnClickListener(new myOnClickListener()); } private class myOnClickListener implements View.OnClickListener { @Override public void onClick(View view) { switch (view.getId()) { case R.id.sendNotification: setSendNotificationBtn(); break; default: break; } } } public void setSendNotificationBtn () { final NotificationCompat.Builder notification = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle("Music Download") .setContentText("burning.mp3") .setNumber(++numMessage); Intent intent = new Intent(this, Notification.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); notification.setContentIntent(pendingIntent); final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); new Thread( new Runnable(){ @Override public void run() { for(int cnt=0; cnt<=100; cnt++){ notification.setProgress(100, cnt, false); manager.notify(mId, notification.build()); try { Thread.sleep(100); } catch (InterruptedException e) { Log.d(TAG, "Sleep failure"); } } notification.setContentText("Download complete"); notification.setProgress(0, 0, false); Uri soundUri = Uri.fromFile(new File("/system/media/audio/animationsounds/bootSound.ogg")); notification.setSound(soundUri); long[] vibrates = {0, 1000, 1000, 1000}; notification.setVibrate(vibrates); manager.notify(mId, notification.build()); } } ).start(); }}
這裡加上了setSound和setVibrate方法,而且須要在AndroidManifest中加入許可權:
<uses-permission android:name="android.permission.VIBRATE"/>
這裡的歌曲名是通過adb shell查看系統的存在的音樂:
下載到手機執行後就能夠觀察效果。
當然還能夠控制led燈,不知為啥led燈的效果一直沒有,網上翻閱非常多資料也沒找到問題所在,若有朋友知道。麻煩告知一二不甚感激。
notification.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); long[] vibrates = {0, 1000, 1000, 1000}; notification.setVibrate(vibrates); notification.setLights(Color.GREEN, 1000, 1000); 關於Notification基本上就學到這裡了。
Android開發學習之路--Notification之初體驗