Notification頂部狀態列和下拉工作列通知
阿新 • • 發佈:2019-02-17
介面很簡單,功能也實現的很簡單,
一個簡單的DEMO,
歡迎轉載,請加地址http://blog.csdn.net/jing110fei/article/details/39055809
1主頁面
2點選開啟發送後
設定每隔10秒傳送1次直到使用者點選通知欄或點選停止傳送
點選通知欄,會跳轉
public class AllApplication extends Application{ public ScheduledExecutorService scheduledThreadPool; @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); scheduledThreadPool=null; } }
為了使這個Application生效,需要在AndroidManifest.xml中替換原來的
<application
android:name=".AllApplication"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
</application>
不多說,直接上程式碼,註釋就在程式碼裡
首先是兩個簡單的佈局檔案
activity_notification_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/Button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="開啟發送通知欄"/> <Button android:id="@+id/Button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="停止傳送通知欄"/> </LinearLayout>
activity2.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/setbar_bg"
/>
</LinearLayout>
主檔案NotificationMainActivity.java
public class NotificationMainActivity extends Activity implements OnClickListener {
private static Button button1,button2;
private AllApplication application;
public NotificationManager mNotificationManager;
//定義count為通知內容用以測試
public int count=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification_main);
// 獲取全域性上下文物件
application=(AllApplication) getApplication();
button1=(Button)findViewById(R.id.Button1);
button1.setOnClickListener(this);
findViewById(R.id.Button2).setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.Button1:
count=0;
/**建立一個可安排在給定延遲後執行命令或者定期地執行的執行緒池。 效果類似於Timer定時器
* ScheduledThreadPool是一個固定大小的執行緒池,與FixedThreadPool類似,執行的任務是定時執行。
* */
application.scheduledThreadPool = Executors.newScheduledThreadPool(1);
//5秒後執行,以後每10秒執行一次
application.scheduledThreadPool.scheduleWithFixedDelay(new CustomTask(), 5, 10,
TimeUnit.SECONDS);
break;
case R.id.Button2:
application.scheduledThreadPool.shutdown();
application.scheduledThreadPool=null;
//取消通知欄顯示
mNotificationManager.cancel(100);
break;
}
}
class CustomTask implements Runnable {
public void run() {
// TODO Auto-generated method stub
Bundle date=new Bundle();
date.putInt("count", count++);
Message message = Message.obtain(sHandler, 1);
message.setData(date);
message.sendToTarget();
}
}
private Handler sHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch (msg.what) {
case 1:
int counts=msg.getData().getInt("count");
mNotificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//手機最上方提示欄的內容
Notification notification = new Notification(R.drawable.ic_launcher,
"count值發生變化當前為"+count, System.currentTimeMillis());
/**
* 設定新增聲音
* 或者使用以下幾種方式
* notification.sound = Uri.parse("file:///sdcard/notification/ringer.mp3");
* notification.sound = Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "6");
* 如果想要讓聲音持續重複直到使用者對通知做出反應,則可以在notification的flags欄位增加"FLAG_INSISTENT"
* 如果notification的defaults欄位包括了"DEFAULT_SOUND"屬性,則這個屬性將覆蓋sound欄位中定義的聲音
* 還可以新增震動 notification.defaults |= Notification.DEFAULT_VIBRATE;
* 或者可以定義自己的振動模式:
* long[] vibrate = {0,100,200,300}; //0毫秒後開始振動,振動100毫秒後停止,再過200毫秒後再次振動300毫秒
* notification.vibrate = vibrate;
* */
notification.defaults |= Notification.DEFAULT_SOUND;
/**
* notification.flags |= FLAG_AUTO_CANCEL; //在通知欄上點選此通知後自動清除此通知
* notification.flags |= FLAG_INSISTENT; //重複發出聲音,直到使用者響應此通知
*/
notification.flags = Notification.FLAG_AUTO_CANCEL;
//通知欄的title
CharSequence contentTitle = "測試";
//通知欄的內容
CharSequence contentText="count值發生變化快去看看吧";
//當點選通知欄時給其設定一個跳轉的頁面
Intent notificationIntent = new Intent();
notificationIntent.setClass(getApplicationContext(),
Activity2.class);
//在這裡傳值給點選跳轉的Intent
notificationIntent.putExtra("count", counts);
PendingIntent contentIntent = PendingIntent.getActivity(
getApplicationContext(), 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
//將上面的設定加入通知欄
notification.setLatestEventInfo(getApplicationContext(),
contentTitle, contentText, contentIntent);
// 用mNotificationManager的notify方法通知使用者生成標題欄訊息通知
//這裡的ID100,用來定義狀態列通知以及下拉工作列通知,並且用來在需要的時候,將這兩樣取消
mNotificationManager.notify(100, notification);
break;
default:
break;
}
}
};
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
點選跳轉後的檔案Activity2.java
public class Activity2 extends Activity{
private AllApplication application;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
application=(AllApplication) getApplication();
application.scheduledThreadPool.shutdown();
application.scheduledThreadPool=null;
setContentView(R.layout.activity2);
Bundle bundle=getIntent().getExtras();
int count=bundle.getInt("count");
TextView textView=(TextView)findViewById(R.id.text1);
textView.setText("當前count值為"+count);
}
}
好了,全部程式碼都在這裡了。。。有不足的地方歡迎大家指正