2013-06-18 218 views
0

我已經構建了一個應用程序,該應用程序應該按照指定的時間間隔發送包含數據使用情況的SMS - 一次是應用程序第一次啓動時 - 然後每分鐘再次發送一次(簡單地縮短爲一分鐘出於測試的目的),但只有inital短信發送,所以它似乎警報不會過期,並啓動我的意圖。AlarmManager無法啓動服務

P.S.

經過CodeMagic的廣泛測試 - 我們仍然無法讓鬧鐘過期並啓動服務。

有什麼建議嗎?

來源:

public class WifiMonitor extends Activity { 

    Button sendButton; 

    EditText msgTextField; 

    private PendingIntent pendingIntent; 

    private Date myDate; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     TextView infoView = (TextView) findViewById(R.id.traffic_info); 


     // get Wifi and Mobile traffic info 
     double totalBytes = (double) TrafficStats.getTotalRxBytes() 
       + TrafficStats.getTotalTxBytes(); 
     double mobileBytes = TrafficStats.getMobileRxBytes() 
       + TrafficStats.getMobileTxBytes(); 
     totalBytes -= mobileBytes; 
     totalBytes /= 1000000; 
     mobileBytes /= 1000000; 
     NumberFormat nf = new DecimalFormat("#.##"); 
     String totalStr = nf.format(totalBytes); 
     String mobileStr = nf.format(mobileBytes); 
     String info = String.format(
       "Wifi Data Usage: %s MB\tMobile Data Usage: %s MB", totalStr, 
       mobileStr); 
     infoView.setText(info); 

     // send traffic info via sms 
     SmsManager smsManager = SmsManager.getDefault(); 
     smsManager.sendTextMessage("7865555555", null, info, null, null); 
     String alarm = Context.ALARM_SERVICE; 

     // get the current date 
     Date date = new Date(System.currentTimeMillis()); 

     // convert the date to milliseconds 
     long millis = date.getTime(); 

     // save the date to shared preferences 
     SharedPreferences prefs = PreferenceManager 
       .getDefaultSharedPreferences(getApplicationContext()); 

     myDate = new Date(prefs.getLong("time", 0)); 
    } 

    // set the alarm to expire 30 days from the date stored in sharePreferences 


    public void invokeAlarm(long invokeTime, long rowId) { 

     Calendar cal = Calendar.getInstance(); 
     cal.setTime(myDate); 

     AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); 
     Intent i = new Intent(this, Alarm.class); 
     i.putExtra("rowId", String.valueOf(rowId)); 
     long waitTime = 1000*10*1; 
       am.setRepeating(AlarmManager.RTC_WAKEUP, invokeTime, waitTime, PendingIntent.getService(
         this, 0, i, 0)); 
     PendingIntent pi = PendingIntent.getService(this, 0, i, 0); 
     am.setRepeating(AlarmManager.RTC_WAKEUP, invokeTime, waitTime, pi); 


    } 

} 

報警:codeMagic最近響應之後

public class Alarm extends Service { 


    // compat to support older devices 
    @Override 
    public void onStart(Intent intent, int startId) { 
     onStartCommand(intent, 0, startId); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 

     // check to ensure everything is functioning 

     Toast toast = Toast.makeText(this, "WiFi Usage Sent", 2000); 
     toast.show(); 

     // send SMS 
     String sms = ""; 
     sms += ("\tWifi Data Usage: " 
       + (TrafficStats.getTotalRxBytes() 
         + TrafficStats.getTotalTxBytes() - (TrafficStats 
         .getMobileRxBytes() + TrafficStats.getMobileTxBytes())) 
       /1000000 + " MB"); 

     SmsManager smsManager = SmsManager.getDefault(); 
     smsManager.sendTextMessage("7865555555", null, sms, null, null); 

     return START_STICKY; 
    } 

    @Override 
    public void onCreate() { 

     // TODO Auto-generated method stub 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 

     // TODO Auto-generated method stub 

     return null; 

    } 

    @Override 
    public boolean onUnbind(Intent intent) { 

     // TODO Auto-generated method stub 

     return super.onUnbind(intent); 

    } 

} 

方法嘗試:

// set the alarm to execute the service 

    public void invokeAlarm(long invokeTime, long rowId) { 

     Calendar cal = Calendar.getInstance(); 
     cal.setTime(myDate); 

     AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE); 
     Intent i = new Intent(getApplicationContext(), Alarm.class); 
     i.putExtra("rowId", String.valueOf(rowId)); 
     long waitTime = 1000 * 10 * 1; 
     am.setRepeating(AlarmManager.RTC_WAKEUP, invokeTime, waitTime, 
       PendingIntent.getService(this, 0, i, 0)); 
     PendingIntent pi = PendingIntent.getService(this, 0, i, 0); 
     try { 
      am.cancel(pendingIntent); 
     } catch (Exception e) { 

     } 
     int timeForAlarm=10000; 
     am.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+timeForAlarm, timeForAlarm,pendingIntent); 

    } 

} 
+0

爲什麼使用是不是你使用[setRepeating](http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating(int,long,long,android.app.PendingIntent))而不是'set() '? – codeMagic

+0

有什麼優勢? –

+0

'setRepeating()'在給定的時間間隔內運行'AlarmManager'。也許我錯過了一些東西,但是就目前而言,我並沒有看到你告訴它每分鐘運行的地方 – codeMagic

回答

0

嘗試改變

am.set(AlarmManager.RTC_WAKEUP, invokeTime, PendingIntent.getService(
      this, (int) System.currentTimeMillis(), i, 0)); 

long waitTime = 1000*60*1 
am.setRepeating(AlarmManager.RTC_WAKEUP, invokeTime, waitTime PendingIntent.getService(
      this, (int) System.currentTimeMillis(), i, 0)); 

cal.add(Calendar.MINUTE, 1);只是增加了1分鐘至當前cal值。而且我不知道有除非我完全失去了一些東西,你正在做的

am.setRepeating(AlarmManager.RTC_WAKEUP, invokeTime, waitTime, PendingIntent.getService(
     this, 0, i, 0)); 

任何理由調用它自己內部invokeAlarm()您可能還需要創建PendingIntentam

PendingIntent pi = PendingIntent.getService(this, 0, i, 0); 
am.setRepeating(AlarmManager.RTC_WAKEUP, invokeTime, waitTime, pi); 
+0

當我做了它導致了2個新錯誤: 令牌「PendingIntent」上的語法錯誤,刪除此令牌 不能調用基本類型的getService(WifiMonitor,int,Intent,int)long –

+0

將'(int)System.currentTimeMillis()'更改爲'int'而不是嘗試投射。只是嘗試使用0 – codeMagic

+0

這看起來是否正確? am.setRepeating(AlarmManager.RTC_WAKEUP,invokeTime,WAITTIME PendingIntent.getService( \t \t \t \t此,整型,0); –