Android學習之------???

雙擊退出

在這裏插入圖片描述

代碼-雙擊退出

public class MainActivity extends AppCompatActivity {

    private long extTime=0;

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

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            Toast.makeText(this,"在點就退出",Toast.LENGTH_SHORT).show();
            if (System.currentTimeMillis() - extTime>=2000){
                extTime=System.currentTimeMillis();
            }else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}

Notification通知

在這裏插入圖片描述

發送一個最簡單的通知(無交互)

在這裏插入圖片描述

代碼

private void sengNotification() {
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.aa);
        builder.setContentTitle("大家好!我是嚶嚶嚶提示");
        builder.setContentText("請大家立即開始嚶嚶嚶");

        Notification build = builder.build();
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1,build);
    }

自定義通知

在這裏插入圖片描述

代碼

private void useerNotification() {
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.aa);
        builder.setContentText("嚶嚶嚶");
        builder.setContentTitle("你喜歡嚶嚶嚶嗎?");


        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.simple_layout);

        remoteViews.setTextViewText(R.id.rm_text_id,"你愛的小姐姐");
        remoteViews.setImageViewResource(R.id.rm_image_id,R.mipmap.aa);

        builder.setCustomBigContentView(remoteViews);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification build = builder.build();


        notificationManager.notify(2,build);

    }

進度條通知

在這裏插入圖片描述

在這裏插入圖片描述

代碼

  private void progress_notification() {
        final NotificationManager manager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        final Notification.Builder builder = new Notification.Builder(this);

        builder.setSmallIcon(R.mipmap.bb);
        builder.setContentTitle("賣竹鼠的發來了一條通知");

        final Timer timer = new Timer();

        timer.schedule(new TimerTask() {
            int progress;
            @Override
            public void run() {
                builder.setContentTitle("正在抓住小姐姐,當前進度為:"+progress);
                builder.setProgress(100,progress,false);
                progress+=10;
                manager.notify(6,builder.build());
                if (progress == 100){
                    builder.setContentText("抓住小姐姐!");
                    builder.setProgress(0,0,true);
                    manager.notify(6,builder.build());

                    try {
                        Thread.sleep(7000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    manager.cancel(6);
                    timer.cancel();
                }
            }
        },0,1000);

    }

通知分組

在這裏插入圖片描述

在這裏插入圖片描述

代碼

  private void groupNotification() {
        NotificationManager manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification.Builder builder = new Notification.Builder(this);
        Notification.Builder builder2 = new Notification.Builder(this);
        Notification.Builder builder3= new Notification.Builder(this);
        Notification.Builder builder4 = new Notification.Builder(this);

        builder.setGroup("2");
        builder.setSmallIcon(R.mipmap.aa);
        builder.setGroupSummary(true);
        builder.setContentText("楊柳依依");
        builder.setContentTitle("昔我往矣");

        builder2.setGroup("2");
        builder2.setSmallIcon(R.mipmap.aa);
        builder2.setContentText("豈因禍福避趨之");
        builder2.setContentTitle("苟利國家生死以");

        builder3.setGroup("2");
        builder3.setSmallIcon(R.mipmap.aa);
        builder3.setContentText("此物最相思");
        builder3.setContentTitle("願君多採擷");

        builder4.setGroup("2");
        builder4.setSmallIcon(R.mipmap.aa);
        builder4.setContentText("朝如青絲暮成雪");
        builder4.setContentTitle("君不見高堂明鏡悲白髮");


        manager.notify(0,builder.build());
        manager.notify(1,builder2.build());
        manager.notify(2,builder3.build());
        manager.notify(3,builder4.build());

    }

PendingIntent

在這裏插入圖片描述

在這裏插入圖片描述

代碼

 private void sendActionNotification() {
        Intent intent=new Intent(this,MainActivity.class);
        PendingIntent pendingIntent=PendingIntent.getActivity(this,0, intent,0);

        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("前不見古人");
        builder.setContentText("後不見來者");


//        builder.setPriority(Notification.PRIORITY_MAX);
//        builder.setDefaults(Notification.DEFAULT_ALL);


//        Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
//        inboxStyle.addLine("屠龍寶刀");
//        inboxStyle.addLine("一刀999級");
//        inboxStyle.addLine("上線就送VIP12");

        Notification.BigPictureStyle bigPictureStyle = new Notification.BigPictureStyle();
        bigPictureStyle.bigLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.aa));

        builder.setStyle(bigPictureStyle);


        builder.setContentIntent(pendingIntent);
        Notification notification = builder.build();

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1,notification);

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