私はアンドロイドコーディングの初心者です。私は、学習演習として mediaplayer プロジェクトに取り組んでいます。メディアプレーヤーをバックグラウンドで再生し、アクティビティがバックグラウンドにプッシュされたときに再生/一時停止ボタンで通知を表示することができました。
これで、アクティビティに onNewIntent() を作成して、通知パネルからアクションを受け取りました。音楽の再生と一時停止はできますが、通知でこれらのボタンをクリックすると、プレーヤーのアクティビティが開きます。アクティビティを開かずに、再生または一時停止アクションのみを実行する方法がわかりません。
私の活動:
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
if (intent.getExtras() != null) {
String tmp;
tmp = intent.getExtras().getString("DO");
if (tmp != null) {
if (tmp.equals("pause")) {
pausePlaying();
}else if(tmp.equals("play")){
startPlaying();
}
else if (tmp.equals("exit")) {
Log.i("onNewIntent", "exit");
finish();
}
}
}
}
NotificationPanel.java (pendingIntents を設定した場所)
public class NotificationPanel{
private Context parent;
private NotificationManager nManager;
private android.support.v4.app.NotificationCompat.Builder nBuilder;
private RemoteViews remoteView;
public NotificationPanel(Context parent) {
// TODO Auto-generated constructor stub
this.parent = parent;
nBuilder = new android.support.v7.app.NotificationCompat.Builder(parent)
.setContentTitle("Transistor")
.setSmallIcon(R.mipmap.ic_launcher)
.setOngoing(true);
remoteView = new RemoteViews(parent.getPackageName(), R.layout.activity_notification_return_slot);
//set the button listeners
setListeners(remoteView);
nBuilder.setContent(remoteView);
nManager = (NotificationManager) parent.getSystemService(Context.NOTIFICATION_SERVICE);
nManager.notify(2, nBuilder.build());
}
public void setListeners(RemoteViews view){
//listener 1
Intent pauseIntent = new Intent(parent,ChannelActivity.class);
pauseIntent.putExtra("DO", "pause");
pauseIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pauseIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
pauseIntent.setAction(Intent.ACTION_MAIN);
pauseIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent btn1 = PendingIntent.getActivity(parent, 0, pauseIntent, 0);
view.setOnClickPendingIntent(R.id.btn1, btn1);
...
//Similarly Intents are set for other actions
}
}
私は解決策を長い間探しました。見つかりませんでした。私の探し方が悪かったのかもしれません。これに関する助けをいただければ幸いです。ティア。