[Android]新增”提醒”

首頁 >> Research >> Android >> [Android]新增”提醒”

1.透過FILE->New->Class 新增一個SERVICE:  g_notify

2.透過FILE->New->Class 新增一個Activity:  response_notify

3.到AndroidManifest.xml-application node裡面新增這兩個元件

4.到g_notify裡面設定service

public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();

String test_notify=Context.NOTIFICATION_SERVICE;
NotificationManager test_notify_manager=(NotificationManager)getSystemService(test_notify);

CharSequence tickerText=”測試notify”;  //notify出現在狀態列時的文字
long when= System.currentTimeMillis();

Notification notify=new Notification(R.drawable.test,tickerText,when);//設定顯示的ICON圖片 . 標題文字
Context c1=getApplicationContext();

//拉下狀態列之後顯示的項目
CharSequence c_title=”提醒”;
CharSequence cText=”點擊進入畫面”;
//拉下狀態列之後顯示的項目–end

Intent cIntent=new Intent(this,response_notify.class);  //設定點擊之後呼叫的類別
PendingIntent PIntent=PendingIntent.getActivity(this, 0, cIntent, 0);

notify.setLatestEventInfo(c1, c_title, cText, PIntent);
test_notify_manager.notify(1,notify);

}

5.新增一個Android XML : test_notify_xml.xml並在response_notify.java裡面將其載入

protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

setContentView(R.layout.test_notify_xml);  //載入
}

6.在主Activity設定載入Service

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//add for notify
Intent i=new Intent(this,g_notify.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startService(i);
//add for notify–end

}

7.大功告成:

8. 比起之前介紹的複雜了點   但花點時間應該是可以理解的

 



================================
分享與讚美,是我們繼續打拼的原動力.
若文章對您有幫助,望請不吝按讚或分享.
或者對影片有興趣可以訂閱頻道接收通知
================================
YouTube 頻道
FB 粉絲專頁
================================

guangyaw

重點主題: 程式設計: Python , Django,Android 工具與軟體: Open edX,Linux工具,Blender教學 分享各地美景與產品使用心得,遊戲實況,甚至影視戲劇等, 您的訂閱就是頻道成長的原動力。 YouTube 頻道: https://youtube.com/xyawli

You may also like...

發表迴響