Notification和鎖屏界面

鎖屏界面app

public void doLockScreen(View view) {

    setUpRemoteControlClient();
    updateRemoteControlClientMetadata();
    showNotification(); // 不顯示notification沒法顯示鎖屏
}

public void setUpRemoteControlClient() {

    Context context = App.getAppContext();

    audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

    ComponentName componentName = new ComponentName(getPackageName(), RemoteControlClientReceiver.class.getName());
    audioManager.registerMediaButtonEventReceiver(componentName);

    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    mediaButtonIntent.setComponent(componentName);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, mediaButtonIntent, 0);

    remoteControlClient = new RemoteControlClient(pendingIntent);
    audioManager.registerRemoteControlClient(remoteControlClient);

    audioManager.requestAudioFocus(new AudioManager.OnAudioFocusChangeListener() {
        
        public void onAudioFocusChange(int focusChange) {
            Log.d(TAG, "focusChange = " + focusChange);
        }
    }, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);


    remoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);

    int flags = RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
            | RemoteControlClient.FLAG_KEY_MEDIA_NEXT
            | RemoteControlClient.FLAG_KEY_MEDIA_PLAY
            | RemoteControlClient.FLAG_KEY_MEDIA_PAUSE
            | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE
            | RemoteControlClient.FLAG_KEY_MEDIA_STOP;
    remoteControlClient.setTransportControlFlags(flags);
}

 @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
private void updateRemoteControlClientMetadata() {
    if (remoteControlClient != null) {
        RemoteControlClient.MetadataEditor editor = remoteControlClient.editMetadata(true);
       
        Bitmap cover = BitmapFactory.decodeResource(getResources(), R.drawable.leak_canary_icon);
        editor.putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK, ((cover != null) ? cover.copy(cover.getConfig(), false) : null));
        editor.apply();
    }
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void showNotification() {
    try {

        NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

        Notification notification;

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.leak_canary_icon)
                .setTicker( "哈哈哈 - 噩噩噩噩")
                .setAutoCancel(false)
                .setOngoing(false);

        Intent notificationIntent = new Intent(this, Main3Activity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

		RemoteViews view = new RemoteViews(getPackageName(), R.layout.notification);
		view.setImageViewBitmap(R.id.notification_icon, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_round));
		view.setTextViewText(R.id.notification_txt, "通知內容");
		view.setOnClickPendingIntent(R.id.notification_txt, pendingIntent);

		builder.setCustomContentView(view);
		notification = builder.build();

        notificationManager.notify(12345, notification);
    }
    catch (NoSuchMethodError e){
    }
}

Android 通知欄Notification的整合 全面學習 http://blog.csdn.net/vipzjyno1/article/details/25248021/學習

相關文章
相關標籤/搜索