Android 狀態欄通知 Notification

来源:https://www.cnblogs.com/zhangxuechao/archive/2019/11/05/11801278.html

private NotificationManager manager; private Notification.Builder builder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCrea... ...


    private NotificationManager manager;
    private Notification.Builder builder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.button);
        // 創建一個通知管理類
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        builder = new Notification.Builder(this);

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(MainActivity.this, MainActivity.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);

                builder.setContentIntent(pendingIntent);
                builder.setContentTitle("new Notification here");
                builder.setContentText("hello furong");
                builder.setSmallIcon(R.drawable.a1);
                builder.setTicker("有通知來了"); // 第一次出現在狀態欄
                // 需要用戶授權 android.permission.VIBRATE
                builder.setDefaults(Notification.DEFAULT_ALL); // 所有的提示都預設

                Notification notification = builder.build();
                manager.notify(1000, notification);
            }
        });
    }

添加用戶許可權

image

試驗效果

image

image


您的分享是我們最大的動力!

更多相關文章
  • protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) fin... ...
  • private int year; private int monthOfYear; private int dayOfMonth; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(saved... ...
  • protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) fin... ...
  • @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (B ...
  • 完成共用參數的讀寫public class SharedPreference { private Context context; public SharedPreference(Context context) { // TODO Auto-generated constructor stub t... ...
一周排行
x