0

一定の時間間隔の後にステータスバー通知を表示するサービスを作成しました。 また、電話機の再起動時や電源投入時にサービスを開始するブロードキャスト受信機も作成しました。私が直面している問題は、電話機が再起動したときに通知がバーで表示されますが、その後でアプリケーションが起動されることです。アプリケーションを起動する必要はありません。ユーザーが通知をクリックしたときにのみ起動する必要があります。ステータスバー通知は、電話の開始時にアクティビティを開きます

広いための私のコードは、Receiverをキャスト:通知のため

@Override 
    public void onReceive(Context context, Intent intent) { 



     if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) { 


      try 
      { 
       Intent intent1 = new Intent(context, NotificationService.class); 
       context.startService(intent1); 

      } 
      catch(Exception e) 
      { 

      } 


     } 

    } 

私のコードです:

public static void showNotification(Context context) 
    { 


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

    Notification notification = new Notification(R.drawable.ic_launcher, "Pull Me Down!!", 1000); 
    Intent intent = new Intent(context,X.class); 
    PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0); 
    notification.setLatestEventInfo(context, "I came!!", "My First Notifcation" , pendingIntent); 
    notificationManager.notify(MY_ID, notification); 

    } 

私は私のサービスのonCreateのメソッドの上に呼び出しています。そしてまた私のXのアクティビティクラスでそれを呼び出す:電話が開始したときに通知が表示されますが、数秒後にXの活動も起動する理由

NotificationService.setActivity(StatusBarNotificationActivity.this); 
       startService(new Intent(getApplicationContext(), NotificationService.class)); 
をしかし、ことは知りません。

答えて

1

解決したばかりで、方法の外でNotificationManagerNotificationと宣言しただけで機能しました。

0

「フォアグラウンドサービス」を使用することもできます。フォアグラウンドサービスを使用するには、startForegroundメソッドを呼び出すだけです。

startForeground(NOTIFICATION_ID, notification); 

ただし、通知が常にステータスバーに表示されないようにするには、サービスのライフサイクルを制御する必要があります。この答えがあなたに役立つことを願っています。 :)

関連する問題