2013-04-26 59 views
1

我有一個應用程序,我希望每天早晨8:30在用戶中向用戶顯示通知。一切工作正常。除了一件事。 每次打開應用程序時都會顯示通知。我搜索了很多這個bug,但我不能糾正這個bug ..每次我打開我的應用程序時都會顯示通知

這是我的應用程序的啓動類。

public class xyz extends Activity { 




/** Called when the activity is first created. */ 

@SuppressWarnings("deprecation") 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 




    Intent myIntent = new Intent(xyz.this , NotificationService.class);  
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
    PendingIntent pendingIntent = PendingIntent.getService(xyz.this, 0, myIntent, 0); 
    Calendar calendar = Calendar.getInstance(); 
    calendar.set(Calendar.HOUR_OF_DAY, 8); 
    calendar.set(Calendar.MINUTE, 30); 
    calendar.set(Calendar.SECOND, 00); 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pendingIntent); 


     final ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
     final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo(); 

     if (activeNetwork != null && activeNetwork.isConnected()) { 
      //notify user you are online 
      Thread Timer = new Thread(){    
        public void run() 
        { 
         try{     

          sleep(3000); 

         }catch(Exception e){ 

        e.printStackTrace(); 

        } 

        finally{ 


        Intent openScreen = new Intent(xyz.this , Selection_Page.class); 

        startActivity(openScreen); 

      }  

        }  

        }; 

         Timer.start(); 
     } 
     else { // something in else} }} 

,這是我通知服務類。

public class NotificationService extends Service { 

private NotificationManager mManager; 

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

@Override 
public void onCreate() { 
    super.onCreate(); 
} 

@Override 
public void onStart(Intent intent, int startId) { 
    //super.onStart(intent, startId); 
    // Getting Notification Service 
    mManager = (NotificationManager) this.getApplicationContext() 
      .getSystemService(
        this.getApplicationContext().NOTIFICATION_SERVICE); 
    /* 
    * When the user taps the notification we have to show the Home Screen 
    * of our App, this job can be done with the help of the following 
    * Intent. 
    */ 
    Uri NOtification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), NOtification); 
    r.play(); 


    Intent intent1 = new Intent(this.getApplicationContext(), Selection_Page.class); 

    Notification notification = new Notification(R.drawable.brand_icon, 
      "See my app for you", System.currentTimeMillis()); 

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

    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
      this.getApplicationContext(), 0, intent1, 
      PendingIntent.FLAG_UPDATE_CURRENT); 

    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    notification.setLatestEventInfo(this.getApplicationContext(), 
      "aaa", "See my app for you", 
      pendingNotificationIntent); 

    mManager.notify(0, notification); 
} 

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

請告訴我,我錯過了東西..

在此先感謝..

+0

我想在你的服務,你應該檢查,看看時間是8:30 – JRowan 2013-04-26 05:04:41

+0

它的存在。還有就是在這個沒有錯誤。該通知顯示在8:30 ..但它也只要我打開我的應用程序 – 2013-04-26 05:09:06

+0

就會顯示我認爲你直接打電話給你的啓動器活動。嘗試檢查是否條件時間如果時間是準確的然後打電話給你的通知服務 – 2013-04-26 05:10:05

回答

0

有一件事我注意到,您設置8:30今天警告這是在過去,因爲你不會改變日曆的日常價值。所以請嘗試將日期值更改爲today + 1,然後重試。以下是更新後的代碼與在day值變化:

Intent myIntent = new Intent(xyz.this , NotificationService.class);  
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
    PendingIntent pendingIntent = PendingIntent.getService(xyz.this, 0, myIntent, 0); 
    Calendar calendar = Calendar.getInstance(); 
    calender.setTime(System.currentTimeMillis()); 
    calendar.set(Calendar.DAY_OF_MONTH, calender.get(Calender.DAY_OF_MONTH)+1); 
    calendar.set(Calendar.HOUR_OF_DAY, 8); 
    calendar.set(Calendar.MINUTE, 30); 
    calendar.set(Calendar.SECOND, 00); 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pendingIntent); 

請測試它讓我知道它是如何工作...

+0

新的PendingIntent()這樣就不會觸發警報 – silverFoxA 2015-06-06 18:28:57

0

你需要在這裏介紹一個檢查點。只需將系統時間與要觸發的通知時間進行比較,如果系統時間大於通知時間,則不執行任何操作。否則會通知。

修改您的代碼如下。它會工作。

Intent myIntent = new Intent(xyz.this , NotificationService.class);  
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
PendingIntent pendingIntent = PendingIntent.getService(xyz.this, 0, myIntent, 0); 
long systemTime = System.currentTimeMillis(); 
Calendar calendar = Calendar.getInstance(); 
calendar.set(Calendar.HOUR_OF_DAY, 8); 
calendar.set(Calendar.MINUTE, 30); 
calendar.set(Calendar.SECOND, 00); 
if(systemTime <= calendar.getTimeInMillis()) { 
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000 , pendingIntent); 
} 
else{ 
//do nothing 
} 
相關問題