[英]Handling a custom notification button click in android
我正在開發基本上是音樂應用程序的Android
應用程序。 根據歌曲在音樂播放器中播放時的要求之一, notification
欄上還應顯示一條通知,當設備鎖定時也可以看到該notification
。 現在,我為notification
創建了一個自定義layout
,該layout
具有一些與音樂播放器進行交互的按鈕。 當歌曲播放時,我可以看到通知,但是我無法處理他們的點擊。 我也創建了一個BroadcastReceiver
。 我已經在SO上進行搜索,谷歌搜索並應用了那里給出的許多解決方案,但我仍然無法處理它。我什至經歷了這段視頻來完成它,但是直到現在: 如何在Android中創建自定義通知
我在這里粘貼代碼,請讓我知道我在哪里做錯了。
這是設置notification
的方法。
public void setNotification(String songName,final String image,String songNamear){
String ns = Context.NOTIFICATION_SERVICE;
notificationManager = (NotificationManager) getSystemService(ns);
Thread th=new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL(image);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
image12 = BitmapFactory.decodeStream(input, null, options);
BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(input, false);
image12 = decoder.decodeRegion(new Rect(10, 10, 50, 50), null);
} catch(IOException e) {
System.out.println(e);
}
}
});
th.start();
Notification.Builder mNotifyBuilder = new Notification.Builder(this);
notification = mNotifyBuilder.setContentTitle(songName)
.setContentText(songNamear)
.setSmallIcon(R.drawable.app_icon)
.setLargeIcon(image12)
.build();
mNotifyBuilder.setPriority(Notification.PRIORITY_HIGH);
notificationView = new RemoteViews(getPackageName(), R.layout.notification_mediacontroller);
notificationView.setImageViewResource(R.id.btn_play_pause_in_notification,R.drawable.home_stop_icon);
Log.d("imageuri",""+Uri.parse(image));
notificationView.setImageViewBitmap(R.id.img_user,image12);
notificationView.setTextViewText(R.id.txt_songname,songName);
notificationView.setTextViewText(R.id.txt_songnamear,songNamear);
SharedPreferences.Editor editor = getSharedPreferences("juke1", MODE_PRIVATE).edit();
editor.putString("pos",""+intPOs);
editor.commit();
Intent notificationIntent = new Intent(this, MusicPlayerActivity.class);
notificationIntent.putExtra("Position",intPOs);
notificationIntent.putExtra("page","not");
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentView = notificationView;
notification.contentIntent = pendingNotificationIntent;
notification.flags |= Notification.FLAG_NO_CLEAR;
//this is the intent that is supposed to be called when the button is clicked
Intent switchIntent = new Intent(Constants.NOTIFICATION_CLICK_ACTION);
switchIntent.setAction(Constants.ACTION_PLAY);
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(MusicPlayerActivity.this, 100, switchIntent, 0);
Intent switchIntent1 = new Intent(Constants.NOTIFICATION_CLICK_ACTION);
switchIntent1.setAction(Constants.ACTION_NEXT);
PendingIntent pendingSwitchIntent1 = PendingIntent.getBroadcast(MusicPlayerActivity.this, 101, switchIntent1, 0);
Intent switchIntent2 = new Intent(Constants.NOTIFICATION_CLICK_ACTION);
switchIntent2.setAction(Constants.ACTION_PREV);
PendingIntent pendingSwitchIntent2 = PendingIntent.getBroadcast(MusicPlayerActivity.this, 102, switchIntent2, 0);
Intent switchIntent3 = new Intent(Constants.NOTIFICATION_CLICK_ACTION);
switchIntent3.setAction(Constants.ACTION_CLOSE);
PendingIntent pendingSwitchIntent3 = PendingIntent.getBroadcast(MusicPlayerActivity.this, 103, switchIntent3, 0);
notificationView.setOnClickPendingIntent(R.id.btn_play_pause_in_notification, pendingSwitchIntent);
notificationView.setOnClickPendingIntent(R.id.btn_next, pendingSwitchIntent1);
notificationView.setOnClickPendingIntent(R.id.btn_prev, pendingSwitchIntent2);
notificationView.setOnClickPendingIntent(R.id.btn_close, pendingSwitchIntent3);
notificationManager.notify(1, notification);
以下是BroadcastReceiver
。 我嘗試了它的單獨類以及MusicPlayerActivity
內部class
。 隨着內部class
,我會很容易調用functions
為下一曲,上一曲,播放/暫停等,為這些buttons
clicks
的目的。
public class NotificationListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
MakeToast.show("Clcik");
String action = intent.getAction();
if(action.equalsIgnoreCase(Constants.ACTION_NEXT)){
MakeToast.show("Next");
}
if(action.equalsIgnoreCase(Constants.ACTION_PREV)){
MakeToast.show("Previous");
}
if(action.equalsIgnoreCase(Constants.ACTION_CLOSE)){
MakeToast.show("Close");
}
if(action.equalsIgnoreCase(Constants.ACTION_PLAY)){
MakeToast.show("Play");
}
}
}
在Android Manifest
,接收方聲明如下。 目前,它是在單獨的class
。
<receiver android:name=".listeners.NotificationListener">
<intent-filter>
<action android:name="notification_button_clicked" />
</intent-filter>
</receiver>
任何幫助將不勝感激,因為我很晚了我的時間表。
謝謝
您可以通過通知遠程視圖使用服務類來處理按鈕單擊。
public class MusicPlayerService extends Service {
public static final String ACTION_PAUSE_PLAY = PACKAGE_NAME + ".playpause";
public static final String ACTION_NEXT = PACKAGE_NAME + ".next";
public static final String ACTION_PREVIOUS = PACKAGE_NAME + ".prev";
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null && intent.getAction() != null) {
switch (intent.getAction()) {
case ACTION_PREVIOUS:
// go to previous song
break;
case ACTION_NEXT:
// go to next song
break;
case ACTION_PAUSE_PLAY:
// pause or play song
break;
}
}
return START_NOT_STICKY;
}
另外,您還必須將通知按鈕與待定意圖鏈接
PendingIntent pendingIntent;
final ComponentName serviceName = new ComponentName(service, MusicPlayerService.class);
// Previous track
pendingIntent = buildPendingIntent(service, ACTION_PREVIOUS, serviceName);
notificationLayout.setOnClickPendingIntent(R.id.prev, pendingIntent);
// Play and pause
pendingIntent = buildPendingIntent(service, ACTION_PAUSE_PLAY, serviceName);
notificationLayout.setOnClickPendingIntent(R.id.playpause, pendingIntent);
// Next track
pendingIntent = buildPendingIntent(service, ACTION_NEXT, serviceName);
notificationLayout.setOnClickPendingIntent(R.id.next, pendingIntent);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.