2016-09-29 58 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

相關問題