2016-11-07 6 views
0

私は通知の時刻を表示し、ストップウォッチを開始および停止することができるカップルのボタンを与える簡単なストップウォッチアプリを作ろうとしています。通知のボタンを作成する方法 '何か'

通知にボタンを追加するにはどうすればよいですか?そして、そのボタンを特定の機能にどのように指し示すのですか?私が考えていたものの相続人は絵

http://image.prntscr.com/image/a4113f39cbaf46c597dc32f07bcc531c.png

actionIntent = new Intent(this, MainActivity.class); 
    actionPendingIntent = PendingIntent.getService(this, 0, actionIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    timerNotification.addAction(android.R.drawable.ic_media_pause, "Start", actionPendingIntent); 

は、これは私が現在持っているものです。私は実行したい機能をどこに置くのですか?

答えて

0

は、カスタムあなたの通知レイアウトにしたい場合は、カスタムレイアウトのRemoteViewsでのsetContent()関数を使用することができ、通知にアクションを追加し、pendingintent

+0

私は自分の質問を更新しました。保留中の意図のどこに機能を置くのですか? –

0

を割り当てます。

Remote View mRemoteView = new RemoteViews(getPackageName(), R.layout.notification_general); 
Notification.Builder mBuilder = new Notification.Builder(context); 
mBuilder.setSmallIcon(R.mipmap.ic_battery) 
     .setContent(mRemoteView) 
     .setContentIntent(notificationPendingIntent); 
mNotificationManager.notify(1, mBuilder.build()); 

通知ボタンonClickイベントを処理するには、すべてのボタンのために(differecntアクションを持つインテントから作られた)別のPendingIntentsを使用する必要があります。後でonReceive()のインテント&の動作をチェックするだけで、それに応じて異なるコードを実行します。マニフェストにリスナーを割り当てることを忘れないでください。

Intent generalIntent = new Intent(context, GeneralReceiver.class); 
generalIntent.putExtra(REQUEST_CODE, ACTION_GENERAL); 
PendingIntent generalPendingIntent = 
     PendingIntent.getBroadcast(context, 1, generalIntent, 0); 
mRemoteView.setOnClickPendingIntent(R.id.btnNotificationGeneral, generalPendingIntent); 
public static class GeneralReceiver extends BroadcastReceiver { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      //Your code here 
     } 
    } 
関連する問題