2013-12-15 4 views
1

상태 표시 줄에 특정 시간에 알림을 시작하는 앱이 있습니다.하지만 앱이 시작될 때마다 알림을 다시 시작합니다.Android : 특정 시간의 상태 표시 줄 알림

미리 감사드립니다.

활동 : MainActivity

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main); 


     //notifications with solve problem 

     Calendar calendar = Calendar.getInstance(); 


     calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); 

     calendar.set(Calendar.HOUR_OF_DAY, 17); 
     calendar.set(Calendar.MINUTE, 30); 
     calendar.set(Calendar.SECOND, 0); 


     calendar.getTimeInMillis(); 


     long current_time = System.currentTimeMillis(); 



     long limit_time = calendar.getTimeInMillis(); 

     if (current_time > limit_time) { 
      //nothing 
     } else { 
      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); 

     } 

     } 

    } 

활동 : MyReceiver

public class MyReceiver extends BroadcastReceiver{ 


@Override 
public void onReceive(Context context, Intent intent) 
{ 
    Intent service1 = new Intent(context, MyAlarmService.class); 
    context.startService(service1); 

    } 
} 

활동 : MyAlarmService

public class MyAlarmService extends Service{ 

    private NotificationManager mManager; 

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

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

    @SuppressWarnings("static-access") 
    @Override 
    public void onStart(Intent intent, int startId) 
{ 
    super.onStart(intent, startId); 

    mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE); 
    Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class); 

    Notification notification = new Notification(R.drawable.ic_launcher,"Run this app", 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(), "Horoscope", "Run this app", pendingNotificationIntent); 

    mManager.notify(0, notification); 
} 

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

} 

내가 probl를 해결 여기

코드입니다 이 게시물에 EM - Notification at specific time

답변

0

사용하는 대신

alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent); 
+0

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), SYNC_REPEAT_MILLSECONDS, pendingIntent); 

이 도움을하지 work.Thanks 않습니다. –

관련 문제