前言
在上一篇文章中《Notification自定義界面》中我們實現了自定義的界面,那么我們該怎么為自定義的界面添加點擊事件呢?像酷狗在通知欄 有“上一首”,“下一首”等控制按鈕,我們需要對按鈕的點擊事件進行響應,不過方法和之前的點擊設置不一樣,需要另外處理,下面我將進行簡單的說明。
實現
同樣,我們需要一個Service的子類MyService,然后在MyService的onCreate中設置,如下代碼:
public class MyService extends Service { public static final String ONCLICK = "com.app.onclick"; private BroadcastReceiver receiver_onclick = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(ONCLICK)) { Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); vibrator.vibrate(1000); } } }; @Override public void onCreate() { super.onCreate(); Notification notification = new Notification(R.drawable.ic_launcher, "JcMan", System.currentTimeMillis()); RemoteViews view = new RemoteViews(getPackageName(),R.layout.notification); notification.contentView = view; IntentFilter filter_click = new IntentFilter(); filter_click.addAction(ONCLICK); //注冊廣播 registerReceiver(receiver_onclick, filter_click); Intent Intent_pre = new Intent(ONCLICK); //得到PendingIntent PendingIntent pendIntent_click = PendingIntent.getBroadcast(this, 0, Intent_pre, 0); //設置監(jiān)聽 notification.contentView.setOnClickPendingIntent(R.id.btn,pendIntent_click); //前臺運行 startForeground(1, notification); } @Override public IBinder onBind(Intent intent) { return null; } }
標題名稱:Android自定義Notification添加點擊事件-創(chuàng)新互聯(lián)
文章路徑:http://aaarwkj.com/article12/iccgc.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供外貿建站、域名注冊、Google、用戶體驗、自適應網站、建站公司
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)