私はアンドロイドが初めてで、助けが必要です。
私は音楽プレーヤーのターゲットAPI 10を実行しています。バックグラウンドで音楽を再生するサービスがあり、再生ボタン、次の曲などのグラフィカルな再生を表示するアクティビティにバインドされています。
アクティビティでは onKeyDown(int keycode, KeyEvent event ) をオーバーライドするので、ホーム ボタンまたはメニュー ボタンをクリックすると通知が作成されます。
次のような通知を作成しました。
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.play_notify)
.setContentTitle(songsList.get(currentSongIndex))
.setContentText("Hello World!");
mBuilder.setAutoCancel(true);
Intent resultIntent = new Intent(this, PlayerActivity.class);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent resultPendingIntent =PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
問題は、(メニューとホーム ボタンだけでなく) 戻るボタンも押されたときにこれを実行したいということですが、機能しません...アクティビティの nullPointerExeption がサービスにバインドされているため、アプリケーションを強制的に閉じます。
バックプレスで通知が作成されることに言及する必要があります。問題は、通知をクリックしてアクティビティを返すときです...
all メソッドを聞いてください:
@Override
public boolean onKeyDown(int keycode, KeyEvent event ) {
if(keycode == KeyEvent.KEYCODE_MENU || keycode == KeyEvent.KEYCODE_HOME || keycode==KeyEvent.KEYCODE_BACK){
if (mServ.isPlaying()){
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.play_notify)
.setContentTitle(songsList.get(currentSongIndex))
.setContentText("Hello World!");
mBuilder.setAutoCancel(true);
Intent resultIntent = new Intent(this, PlayerActivity.class);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent resultPendingIntent =PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
// Sets an ID for the notification
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());
if (keycode == KeyEvent.KEYCODE_BACK){
super.onBackPressed () ;
}
else
moveTaskToBack(true);
}
return super.onKeyDown(keycode,event);
}
return false;
}
前もって感謝します。