![](/img/trans.png)
[英]Intent Activity Refreshes Instead of Opening New Activity Upon Button Click
[英]On notification button click intent starting new activity instead of resuming
我正在嘗試通過通知欄為我的媒體播放器實現自定義通知,該通知欄只有一個按鈕,可以用作停止和播放控件。
到目前為止,已經成功實現了通知,並且按鈕正在調用函數,但是問題是,當我創建一個意圖並調用活動時,onReceive會在舊的活動之上重新創建活動,並且我得到了不好的雙重回聲媒體播放器在后台播放。
嘗試將launchMode = Single設置為單個,但是當我將其設置為Single時,單擊按鈕沒有區別,這意味着如果我將啟動模式設置為SINGLE而不是STANDARD,則不會進行函數調用。
MAIN_ACTIVITY代碼片段
//NOTIFICATION RELATED CLASSES
private NotificationCompat.Builder builder;
private NotificationManager notificationManager;
//private int notification_id;
private RemoteViews remoteViews;
Context context;
Intent notification_intent;
final int notification_id =545816666;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean attachMedia = getIntent().getBooleanExtra("attachMedia",false);
if (attachMedia) {
attachMediaActivity();
}
//CODE DE NOTIFICATION
context =this;
notification_intent=new Intent(context,MainActivity.class);
notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
remoteViews=new RemoteViews(getPackageName(),R.layout.custom_notification);
remoteViews.setImageViewResource(R.id.notif_icon,R.mipmap.ic_launcher);
remoteViews.setTextViewText(R.id.notif_title,"BOOM");
remoteViews.setTextViewText(R.id.button,"Button");
Intent button_intent= new Intent("player_control_clicked");
button_intent.putExtra("id",notification_id);
PendingIntent p_button_intent=PendingIntent.getBroadcast(context,123,button_intent,0);
remoteViews.setOnClickPendingIntent(R.id.button,p_button_intent);
tview=(TextView) findViewById(R.id.playerText);
btn=(Button) findViewById(R.id.playerButton);
if(!mediaPlayer.isPlaying())
{
tview.setText("Press play");
btn.setText("Play");
}
else
{
tview.setText("Playing");
btn.setText("Stop");
}
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Intent notification_intent=new Intent(context,MainActivity.class);
//THIS CODE UPDATES OLD NOTIFICATION
PendingIntent pendingIntent=PendingIntent.getActivity(context,0,notification_intent,PendingIntent.FLAG_UPDATE_CURRENT);
builder =new NotificationCompat.Builder(context);
builder.setSmallIcon(R.mipmap.ic_launcher)
.setCustomBigContentView(remoteViews)
.setContentIntent(pendingIntent)
.setOngoing(true);
notificationManager.notify(notification_id,builder.build());
if(!mediaPlayer.isPlaying())
{
//tview.setText("Playing");
btn.setText("Stop");
playStream();
}
else
{
tview.setText("Stopped");
btn.setText("Play");
mediaPlayer.stop();
mediaPlayer.reset();
}
}
});
}
public void attachMediaActivity()
{
//CODE DE NOTIFICATION
context =this;
notification_intent=new Intent(context,MainActivity.class);
notificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
remoteViews=new RemoteViews(getPackageName(),R.layout.custom_notification);
remoteViews.setImageViewResource(R.id.notif_icon,R.drawable.stream_icon);
remoteViews.setTextViewText(R.id.notif_title,"Stopped");
remoteViews.setTextViewText(R.id.button,"STOPA");
Intent button_intent= new Intent("player_control_clicked");
button_intent.putExtra("id",notification_id);
/*PendingIntent p_button_intent=PendingIntent.getBroadcast(context,123,button_intent,0);
remoteViews.setOnClickPendingIntent(R.id.button,p_button_intent);*/
PendingIntent pendingIntent=PendingIntent.getActivity(context,0,notification_intent,PendingIntent.FLAG_UPDATE_CURRENT);
builder =new NotificationCompat.Builder(context);
builder.setSmallIcon(R.mipmap.ic_launcher)
.setCustomBigContentView(remoteViews)
.setContentIntent(pendingIntent)
.setOngoing(true);
notificationManager.notify(notification_id,builder.build());
if (mediaPlayer.isPlaying())
{
mediaPlayer.stop();
}
else
{
playStream();
}
}
用於通知按鈕的廣播級收聽者,可通過意圖調用該活動
public class Button_listener extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(intent.getExtras().getInt("id"));
Toast.makeText(context, "GENERATED BY NOTIFICATION", Toast.LENGTH_SHORT).show();
intent= new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//intents.addExtra("attachMedia",true); // Extra info
intent.putExtra("attachMedia",true);
context.startActivity(intent);
}
}
我的清單
<activity android:name=".MainActivity"
android:screenOrientation="portrait"
android:launchMode="singleTop"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="Button_listener">
<intent-filter>
<action android:name="player_control_clicked"/>
</intent-filter>
</receiver>
在此先感謝PS:在堆棧上嘗試了所有可能的答案
問題出在您的意圖標志之內
您已經添加了標志intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
,將其更改為intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
有關更多參考,請閱讀此https://developer.android.com/reference/android/content/Intent.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.