标签:
package com.example.notification; import org.apache.http.client.utils.URIUtils; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.util.Log; import android.view.KeyEvent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.content.DialogInterface; import android.view.ViewDebug.FlagToString; import android.widget.Button; public class MainActivity extends Activity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { newNotification(); } }); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { super.onKeyDown(keyCode, event); if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK) { // 点击了返回键, 阻止界面退出, 并创建对话框 newDialog(); Log.d("onKeyUp", "新建对话框"); return false; } return true; } private Dialog newDialog () { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setIcon(R.drawable.ic_launcher); builder.setTitle("确定关机?"); builder.setMessage("Message...."); builder.setPositiveButton("确定", dialogListener); builder.setNegativeButton("取消", dialogListener); builder.create(); return builder.show(); } // 对话框按钮的监听器 private DialogInterface.OnClickListener dialogListener = new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { if (arg1 == -1) { // 点击了确定键, NegativeButton newNotification(); finish(); Log.d("dialogListener", "新建通知"); } } }; private Notification newNotification () { Notification.Builder builder = new Notification.Builder(MainActivity.this); // 创建位图 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); builder.setSmallIcon(R.drawable.ic_launcher); builder.setLargeIcon(bitmap); builder.setTicker("状态栏文本"); builder.setContentTitle("通知的标题"); builder.setContentText("通知的信息"); builder.setWhen(System.currentTimeMillis()); builder.setAutoCancel(true); Intent intent = new Intent(MainActivity.this, MainActivity.class); PendingIntent pendingItent = PendingIntent.getActivity(MainActivity.this, Intent.FLAG_ACTIVITY_NEW_TASK, intent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pendingItent); // 获取系统的通知管理器 NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = builder.build(); manager.notify(1, notification); return notification; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
标签:
原文地址:http://www.cnblogs.com/thens/p/5286006.html