2015-03-13 35 views
0

我有一個應用程序每天在確切時間處發送notificationAlarmManager每當應用程序在Android中打開時都會發送通知

但它有一個錯誤。 每當應用程序打開時,都會發送通知

我怎樣才能得到的通知是每天發送一次?謝謝

MAINACTIVITY

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(); 

    int d = Integer.valueOf(1440); 
    calendar.setTimeInMillis(System.currentTimeMillis()); 

    Intent i = new Intent(MainActivity.this, Receiver.class); 
    pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, i, 0); 

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * d, pendingIntent); 
} 
} 

接收機

public class Receiver extends BroadcastReceiver { 

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

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); 
    Intent i = new Intent(context, MainActivity.class); 

    Notification notification = new Notification(R.drawable.ic_launcher,"This is a test message!", System.currentTimeMillis()); 

    long[] vibrate = {100, 100, 200, 300}; 
    notification.vibrate = vibrate; 
    notification.defaults = Notification.DEFAULT_ALL; 

    i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(context, 0, i,PendingIntent.FLAG_UPDATE_CURRENT); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    notification.setLatestEventInfo(context, "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent); 

    notificationManager.notify(0, notification); 
} 
} 
+0

比較你兩次。當你打開應用程序那個時間和你的固定時間,並比較兩者。如果它的平等,然後發送通知。 – Piyush 2015-03-13 09:08:21

+0

@PiyushGupta我希望通知是自動發送的,雖然沒有打開這個應用程序。這是否使你的解決方案? – user4152 2015-03-13 09:12:20

+0

但在此之前,您必須在您的應用處於前景時進行檢查。並且您的代碼位於onCreate()方法中,每次打開應用程序或返回到此活動時,都會調用接收器。 – Piyush 2015-03-13 09:13:24

回答

0

代碼來調用CallThisClass.class每int d分鐘

Intent myIntent = new Intent(getActivity(), CallThisClass.class); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent, 0); 
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE); 
Calendar calendar = Calendar.getInstance(); 
int d = Integer.valueOf(duration); 
calendar.setTimeInMillis(System.currentTimeMillis()); 
calendar.add(Calendar.SECOND, 2); 
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * d, pendingIntent); 

代碼以停止呼籲CallThisClass.class

Intent myIntent = new Intent(getActivity(), CallThisClass.class); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent, 0); 
alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE); 
alarmManager.cancel(pendingIntent); 

CallThisClass.class

public class CallThisClass extends BroadcastReceiver { 
    @Override 
    public void onReceive(final Context context, Intent intent) { 
     Log.i("Received", "Broadcast Received !"); 
     // Do your stuff 
    } 
} 

不要忘記接收器inyour AndroidManifest.xml中

<receiver android:name=".CallThisClass" /> 

和權限

<uses-permission android:name="com.android.alarm.permission.SET_ALARM" /> 

可以開始打電話CallThisClass.class只要你想。例如。點擊一個按鈕,或者當你的主要活動開始時。只要確保在不想讓它工作時停止調用該類。例如你想要當用戶第一次安裝你想要的應用程序,每24小時應該有東西更新(通知或其他),rit?在你的prefrences存儲一個變量,只有當它第一次被加載時纔會改變它自己。每次用戶輸入應用程序時檢查該變量。如果是第一次啓動呼叫代碼。 (現在我們不想調用停止代碼,因爲即使應用程序已關閉,我們也不能繼續調用該類)關閉應用程序,它將起作用。你可以在你設置一個按鈕或者進行一些測試,點擊時將運行代碼,以便不調用該類。它會在你的情況下每隔d分鐘停止調用調用類,例如24小時(也可以在此按鈕的clik上將優先級更改爲默認值,以便下次啓動活動或單擊激活或再次啓動時調用該代碼課程開始)。我希望它有幫助。

而且,我建議讀here for Best practices

+0

這段代碼進入Main的onCreate?並且每隔d分鐘重複一次? – user4152 2015-03-13 09:54:02

+0

我編輯了答案。我希望它能幫助你:) – 2015-03-13 10:17:10

+0

我已經用你的代碼編輯了我的代碼。但我有同樣的問題。通知總是發送 – user4152 2015-03-13 17:16:27

相關問題