第二十八天Notification

Notification

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Button mButton1;
    private Button mButton2;
    private Button mButton3;
    private Button mButton4;
    private NotificationManager mNotificationManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mNotificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mButton1= (Button) findViewById(R.id.button1);
        mButton2= (Button) findViewById(R.id.button2);
        mButton3= (Button) findViewById(R.id.button3);
        mButton4= (Button) findViewById(R.id.button4);
        mButton1.setOnClickListener(this);
        mButton2.setOnClickListener(this);
        mButton3.setOnClickListener(this);
        mButton4.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button1:
                showNotification1();
                break;
            case R.id.button2:
                mNotificationManager.cancel(1);//取消通知
                break;
            case R.id.button3:
               showNotification2();
                break;
            case R.id.button4:
                createNotification();
                break;
            default:
                break;
        }
    }

    public void showNotification1(){
        Notification notification=new Notification();
        notification.icon=R.mipmap.ic_launcher;
        notification.tickerText="有一條新消息";
        notification.flags=Notification.FLAG_AUTO_CANCEL;
        Intent intent=new Intent(getApplicationContext(),MainActivity.class);
        PendingIntent pendingIntent=PendingIntent.getActivity(getApplicationContext(),1,intent,PendingIntent.FLAG_ONE_SHOT);
        notification.setLatestEventInfo(getApplicationContext(),"新消息","你好",pendingIntent);
        notification.when=System.currentTimeMillis();
        mNotificationManager.notify(1,notification);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public void showNotification2(){
        Intent intent=new Intent(getApplicationContext(),MainActivity.class);
        PendingIntent pendingIntent=PendingIntent.getActivity(getApplicationContext(),1,intent,PendingIntent.FLAG_ONE_SHOT);
        Notification notification=new Notification.Builder(MainActivity.this).setSmallIcon(R.mipmap.ic_launcher).setTicker("有一條新消息")
                .setContentTitle("消息")
                .setContentText("文本").setContentInfo("你好")
                .setContentIntent(pendingIntent)
                .setAutoCancel(true).setWhen(System.currentTimeMillis()).build();
        mNotificationManager.notify(1,notification);
    }

 /**
 *自定義Notification
 */
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public void createNotification(){
        RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.remoteview_layout);
        Intent intent=new Intent(getApplicationContext(),MainActivity.class);
        PendingIntent pendingIntent=PendingIntent.getActivity(getApplicationContext(),1,intent,PendingIntent.FLAG_ONE_SHOT);
        Notification notification=new Notification.Builder(MainActivity.this).setSmallIcon(R.mipmap.ic_launcher).setTicker("有一條新消息")
                .setContentIntent(pendingIntent)
                .setAutoCancel(true).setWhen(System.currentTimeMillis()).setContent(remoteViews).build();
        mNotificationManager.notify(1,notification);
    }

這裏寫圖片描述


這裏寫圖片描述


這裏寫圖片描述


這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章