1

我有一个应用程序,我想在每天早上 8:30 向用户显示通知。一切正常。除了一件事。每次打开应用程序时都会显示通知。我搜索了很多这个错误,但我无法纠正这个错误..

这是我的应用程序的启动器类。

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

请告诉我我在哪里错过了一些东西..

提前致谢..

4

2 回答 2

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

请测试它让我知道它是如何工作的......

于 2013-04-26T05:55:33.143 回答
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
}
于 2017-03-21T15:52:58.373 回答