2015年12月16日 星期三

Android 5.0 Lollipop Notification Lock Screen Visibility 鎖頻 通知

Android 5.0 Lollipop 通知訊息在鎖頻時,可以設定公開或是私密訊息

1.先設定手機
設定 ->安全性 -> 螢幕鎖定方式 ->選擇圖形、PIN碼或密碼 ->設定密碼完成 ->選擇隱藏敏感通知內容 ->完成

2./res/layout/activity_main.xml


    

3.MainActivity.java
package com.terryyamg.lollipopnotificationtest;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btNotification = (Button) findViewById(R.id.btNotification);

        btNotification.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, Detail.class); //點選後動作
                PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this, (int) System.currentTimeMillis(), intent, 0);
                // addAction(int icon, CharSequence title, PendingIntent intent) Deprecated 的解決方法
                NotificationCompat.Action detailAction =
                        new NotificationCompat.Action.Builder(R.mipmap.ic_launcher, "Detail", pIntent).build();

                NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
                builder.setSmallIcon(R.mipmap.ic_launcher);
                builder.setContentTitle("標題");
                builder.setContentText("內容");
                builder.setOngoing(false); //固定
//              builder.setFullScreenIntent(pIntent,true);//一直顯示在最上層 優先於setPriority
                builder.setPriority(Notification.PRIORITY_DEFAULT); //通知的位置 - Android 5.0 PRIORITY_MIN在底線以下
                builder.setVisibility(NotificationCompat.VISIBILITY_PRIVATE); //顯示訊息-VISIBILITY_PUBLIC 公開,VISIBILITY_PRIVATE私密
                builder.addAction(detailAction); //加入下方按鈕

                NotificationManager notificationManager =
                        (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                notificationManager.notify(0, builder.build());

            }
        });

    }
}
4./res/layout/detail.xml


    


5.Detail.java
package com.terryyamg.lollipopnotificationtest;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Detail extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.detail);

        TextView tvDetail = (TextView)findViewById(R.id.tvDetail);
        tvDetail.setText("跳進來了");
    }
}

設定為 VISIBILITY_PUBLIC時
設定為 VISIBILITY_PRIVATE時

檔案下載:
https://github.com/terryyamg/LollipopNotificationTest
參考連結
http://www.intertech.com/Blog/android-development-tutorial-lollipop-notifications/
https://developer.getpebble.com/blog/2014/12/19/Leverage-Android-Actionable-Notifications/
http://stackoverflow.com/questions/13808939/jelly-bean-the-method-setpriorityint-is-undefined-for-the-type-notification

沒有留言 :

張貼留言