2011-08-31 4 views

답변

2

확인 지금 내가 코드를 업데이트 한 ... 많은 검색하지 않았다.

다음은 CountDownTimer의 코드입니다. 시간이 완료되면 알림을 보냅니다. 서비스 또는 일반적인 활동에서 사용할 수 있습니다.

 public String ns = Context.NOTIFICATION_SERVICE; 
     public NotificationManager mNotificationManager;  
     Notification notification; 
     PendingIntent contentIntent; 
     Intent notificationIntent; 

     public class Main extends Activity{ 
      private final long startTime = 50000; 
      private final long interval = 1000; 

      @Override 
      public void onCreate(Bundle savedInstanceState) 
       { 
        super.onCreate(savedInstanceState); 
          int time=Integer.parseInt(editText.getText().toString()); 
          time=time*1000; 
          startTime = time; 

          // place this code on button listener if you want. 
        countDownTimer = new MyTimer(startTime, interval); 
       } 
     } 

     public class MyTimer extends CountDownTimer 
     { 
      public MyTimer(long startTime, long interval) 
       { 
        super(startTime, interval); 
       } 

       @Override 
       public void onFinish() 
       { 
        mNotificationManager = (NotificationManager) getSystemService(ns); 
        notificationIntent = new Intent(this, MyActivity.class); 
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
        notification = new Notification(R.drawable.icon,"Time up..", System.currentTimeMillis()); 
        notification.setLatestEventInfo(this, "CallingaCab", "YOUR TIME COMPLETED", contentIntent); 
        mNotificationManager.notify(0, notification); 
        startActivity(notificationIntent); 
       } 

       @Override 
       public void onTick(long millisUntilFinished) 
       { 
        //Perform what do you want on each tick. 
       } 
      } 
     } 
+0

카운트 다운 타이머 내에서 활동을 시작하려면 어떻게해야합니까? – molleman

+0

또한 getSystemService에 전달할 수있는 대상은 무엇입니까? NS? – molleman

+0

지금 확인해보십시오. 코드를 업데이트했습니다. – Noby

관련 문제