Notification の通知で、AlertDialog を通知元の Activity 画面の上に表示して終了させる。
ホーム画面でも Notification で、警告して終了させるのが目的で書いてみる
でも。やはりこれはダメだ。
import android.app.Activity;
import android.app.AlertDialog;
import android.app.NotificationManager;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Window;
/**
* TranslucentAlertActivity
*/
public class TranslucentAlertActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Bundle bundle = getIntent().getExtras();
// Bundle → Notification消去の為の ID を通知元から受け取る。
// → Alert表示のタイトル・メッセージを通知元から受け取る。
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(bundle.getInt("NOTIFY_ID"));
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_menu_more)
.setTitle(bundle.getString("TITLE"))
.setMessage(bundle.getString("MESSAGE"))
.setPositiveButton("OK",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which){
// finish() を実行して終了
finish();
}
})
.create().show();
}
}
res/values/styles.xml で透過のThemeを定義
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="translucent_background">#44000000</drawable>
<style name="Theme.MyTranslucent" parent="android:style/Theme.Translucent">
<item name="android:windowBackground">@drawable/translucent_background</item>
</style>
</resources>
AndroidManifest.xml で、TranslucentAlertActivity のTheme を指定する
<activity android:name=".sample.TranslucentAlertActivity"
android:theme="@style/Theme.MyTranslucent">
</activity>
Notification でこの TranslucentAlertActivity を呼び出す通知を行う。
Intent targetIntent = new Intent(context,TranslucentAlertActivity.class);
targetIntent.putExtra("NOTIFY_ID", 6);
targetIntent.putExtra("TITLE" ,"タイトル");
targetIntent.putExtra("MESSAGE" ,"メッセージ");
final PendingIntent pintent = PendingIntent.getActivity(getApplicationContext(), 0, targetIntent, 0);
Notification notification = new Notification(R.drawable.notificate_g,"Tickerメッセージ...",System.currentTimeMillis());
notification.setLatestEventInfo(getApplicationContext(),"タイトル","Alert",pintent);
notificationManager.notify(6,notification);