Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Androidの通知領域ボタンで動作分け(singleTaskで多重起動防止)
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.quitsq.notify3button.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.quitsq.notify3button">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:launchMode="singleTask"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package com.quitsq.notify3button;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v4.app.NotificationCompat;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private static NotificationManager mNotificationManager;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNotificationManager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
mNotificationManager.cancelAll();
sendNotification();
text = (TextView) findViewById(R.id.text);
Intent intent = getIntent();
int type = intent.getIntExtra("type", 0);
text.setText("type:" + type);
doSomething(type);
}
@Override
public void onDestroy() {
super.onDestroy();
mNotificationManager.cancelAll();
}
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
int type = intent.getIntExtra("type", 0);
text.setText("type:" + type);
doSomething(type);
}
private void doSomething(int type) {
switch (type){
case 0:
text.setText("type 0:" + type);
break;
case 1:
text.setText("type 1:" + type);
break;
case 2:
text.setText("type 2:" + type);
break;
case 3:
text.setText("type 3:" + type);
break;
default:
break;
}
}
private void sendNotification() {
Intent intent1 = new Intent(this, MainActivity.class);
intent1.putExtra("type",1);
PendingIntent pendingIntent1 = PendingIntent.getActivity(getApplicationContext(), 1, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
Intent intent2 = new Intent(this, MainActivity.class);
intent2.putExtra("type",2);
PendingIntent pendingIntent2 = PendingIntent.getActivity(getApplicationContext(), 2, intent2, PendingIntent.FLAG_UPDATE_CURRENT);
Intent intent3 = new Intent(this, MainActivity.class);
intent3.putExtra("type",3);
PendingIntent pendingIntent3 = PendingIntent.getActivity(getApplicationContext(), 3, intent3, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle("Title")
.setContentText("text")
.addAction(0,"Type1", pendingIntent1)
.addAction(0,"Type2", pendingIntent2)
.addAction(0,"Type3", pendingIntent3)
.setAutoCancel(false)
.setOngoing(false);
mNotificationManager.notify(0, builder.build());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.