Android status bar notification Notification

    private NotificationManager manager;
    private Notification.Builder builder;

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

        the Button = (the Button) findViewById (R.id.button);
         // create a notification Management 
        Manager = (the 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 ( "notice has come"); // first appears in the status bar
                 // require user authorization android.permission.VIBRATE 
                builder.setDefaults (Notification.DEFAULT_ALL); // all prompts by default 

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

Add user permissions

image

Test results

image

image

Guess you like

Origin www.cnblogs.com/zhangxuechao/p/11801278.html