2017-08-10 27 views
0

因此,當鎖屏在屏幕上時,用戶收到通知應點亮。接收到通知時的喚醒屏幕

private void unlockScreen() { 
    Window window = this.getWindow(); 
    window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); 
    window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); 
    window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); 
} 

我有這個來解鎖屏幕。然後我用這個來稱呼它

private void updateNotification(int notificationCode) 
{ 
    switch(notificationCode) 
    { 
     case NotificationService.InterceptedNotificationCode.FACEBOOK_CODE: 
      shapeRoundButton.setImageResource(R.drawable.toggle_on); 
      unlockScreen(); 
      break; 
     case NotificationService.InterceptedNotificationCode.OTHER_NOTIFICATIONS_CODE: 
      shapeRectangleButton.setImageResource(R.drawable.toggle_on); 
      break; 
    } 
} 

shapeRoundButton.setImageResource(R.drawable.toggle_on)工作完全正常,但unlockScreen();沒有。通知正在收到。

任何人都知道爲什麼屏幕沒有被解鎖或我如何解鎖屏幕?乾杯!

回答

0

看到這個

PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); 
boolean isScreenOn = pm.isScreenOn(); 
Log.e("screen on.................................", ""+isScreenOn); 
if(isScreenOn==false) 
{ 
    WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE,"MyLock"); 
    wl.acquire(10000); 
    WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"MyCpuLock"); 

    wl_cpu.acquire(10000); 
} 
0

嘗試使用IntentService。與你的意圖的服務替換你的意圖的目標:

Intent yepIntent = new Intent(context, MyIntentService.class); 
yepIntent.putExtra("foo", true); 
yepIntent.putExtra("bar", "more info"); 
PendingIntent yepPendingIntent = PendingIntent.getService(context, notificationId, yepIntent, PendingIntent.FLAG_CANCEL_CURRENT); 
notificationBuilder.addAction(R.drawable.icon_of_choice, "My Action", yepPendingIntent); 

註冊服務的清單:

<service 
    android:name="app.great.mypackage.MyIntentService" 
    android:exported="false"/> 

你的服務看起來是這樣的:

public class MyIntentSerice extends IntentService { 
@Override 
protected void onHandleIntent(Intent intent) { 
    Log.d("myapp", "I got this awesome intent and will now do stuff in the background!"); 
    // .... do what you like 
} 
} 

從名稱

反饋UPDATE

詭計似乎是

  1. 使用服務
  2. 添加intent不是作爲action或contentIntent,而是使用RemoteViews方法。

組合時,應:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this) 
    .setVisibility(Notification.VISIBILITY_PUBLIC) 
    .setOngoing(true) 
    .setSmallIcon(R.drawable.abc_ic_menu_share_mtrl_alpha) 
    .setContentTitle("My notification") 
    .setContentText("Hello World!"); 

int notificationId = 1; 
Intent yepIntent = new Intent(this, MyIntentService.class); 
yepIntent.setAction("test"); 
yepIntent.putExtra("foo", true); 
yepIntent.putExtra("bar", "more info"); 
PendingIntent yepPendingIntent = PendingIntent.getService(this, 
notificationId, yepIntent, PendingIntent.FLAG_CANCEL_CURRENT); 

// doesn't show up on my lock-screen 
//builder.addAction(R.drawable.abc_ic_menu_share_mtrl_alpha, "My Action", 
yepPendingIntent); 

// asks for unlock code for some reason 
//builder.setContentIntent(yepPendingIntent); 

// Bingo 
RemoteViews view = new RemoteViews(getPackageName(), R.layout.notification); 
view.setOnClickPendingIntent(R.id.notification_closebtn_ib, yepPendingIntent); 
builder.setContent(view); 
+0

也許應該做出更清楚,但我不是做的通知,但是從應用如Facebook,Instagram的和喜歡接受他們。但是,無論如何,我會繼續努力,看看會發生什麼。 –

相關問題