2016-09-29 4 views
1

自分のシナリオは、アプリを使用していなくてもユーザーに挨拶の通知を表示することです。開いている、または最小化されている場合、以下のコードは正常に動作しています。しかし、ユーザーがアプリケーションを開かなかったにもかかわらず、午前中に通知を表示したい。アプリが実行されていないときに通知を表示する方法

主な活動:

public class MainActivity extends Activity { 

    private PendingIntent pendingIntent; 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Calendar calendar = Calendar.getInstance(); 
     calendar.set(Calendar.HOUR_OF_DAY, 18); 
     calendar.set(Calendar.MINUTE, 40); 
     calendar.set(Calendar.SECOND, 0); 
     Intent myIntent = new Intent(MainActivity.this, MyReceiver.class); 
     pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0); 

     AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
     alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent); 

    } //end onCreate 
} 

マイレシーバクラス:

public class MyReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     /*Intent service1 = new Intent(context, MyAlarmService.class); 
     context.startService(service1);*/ 
     try{ 
      Utils.generateNotification(context); 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
    } 

} 

Utilsのクラス:

public class Utils { 

    public static NotificationManager mManager; 

    @SuppressWarnings("static-access") 
    public static void generateNotification(Context context){ 

     mManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); 
     Intent intent1 = new Intent(context,MainActivity.class); 
     Notification notification = new Notification(R.drawable.ic_launcher,"This is a test message!", System.currentTimeMillis()); 
     intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     PendingIntent pendingNotificationIntent = PendingIntent.getActivity(context,0, intent1,PendingIntent.FLAG_UPDATE_CURRENT); 
     notification.flags |= Notification.FLAG_AUTO_CANCEL; 
     notification.setLatestEventInfo(context, "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent); 
     mManager.notify(0, notification); 
    } 
} 

マイアラームサービス:

public class MyAlarmService extends Service { 

    @Override 
    public IBinder onBind(Intent arg0) 
    { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public void onCreate() 
    { 
     // TODO Auto-generated method stub 
     super.onCreate(); 
    } 

    @Override 
    public void onStart(Intent intent, int startId) 
    { 
     super.onStart(intent, startId); 
    } 

    @Override 
    public void onDestroy() 
    { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
    } 

} 

次の活動:あなたがここに行う必要があり

public class NextActivity extends Activity { 

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

答えて

0

いくつかのこと。ユーザーが強制終了した場合は、START_STICKYでサービスを開始して、アプリケーションが強制終了されたときにサービスが再開するようにする必要があります。

ユーザーが電話を再起動したときにサービスが開始されるようにするには、BroadcastReceiverreceive BOOT_COMPLETEDが必要になり、サービスが開始されます。

あなたのサービスは、通常のようにアラームを設定します。どこかに設定したいアラームの時刻を保存する必要があります。 SharedPrefs(PreferenceManager.getDefaultSharedPreferences(getApplicationContext());)を使用していますが、より良い方法があると確信しています。

+0

Laridコードの例を挙げてください。 – Michael

+0

コードは両方のリンクでかなり明確です。申し訳ありません。幸運 あなたがそれらを見逃した場合:https://developer.android.com/reference/android/app/Service.html#START_STICKY http://stackoverflow.com/questions/5290141/android-broadcastreceiver-on-起動時に実行中のアクティビティがバックグラウンドにある/ 5290164#5290164 –

+0

問題なし@Laridあなたの知識を共有してくれてありがとう:) – Michael

関連する問題