2016-03-28 87 views
0

我創建了簡單的服務,必須每隔一秒記錄一次。我使用了報警管理器(因爲從official documentNote: The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running.),但它隨機重複。 這裏是我的代碼報警管理器在服務中重複出現異常

MainActiviry.class

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     if (fab != null) { 
      fab.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View view) { 
        Intent intent = new Intent(MainActivity.this,LogService.class); 
        startService(intent); 
       } 
      }); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

我通過點擊按鈕FAB啓動服務。

LogService.class

public class LogService extends Service { 
    private static final String TAG = LogService.class.getSimpleName(); 

    public LogService() { 
     super(); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Log.e(TAG, "onStartCommand: Service calling"); 

     return super.onStartCommand(intent, flags, startId); 
    } 


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

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

    @Nullable 
    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    private void Timer(Context context){ 
     Intent intent = new Intent(context,LogService.class); 
     PendingIntent pendingIntent = PendingIntent.getService(context,0,intent,0); 
     AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(getApplicationContext().ALARM_SERVICE); 
     alarmManager.setRepeating(alarmManager.RTC_WAKEUP,System.currentTimeMillis(),1*1000,pendingIntent); 
    } 
} 

這是我LOG

Logcat

它重複的每一分鐘,但我寫的每一秒。我做錯了嗎?

回答

0

由於API 19重複報警是不精確。看到這個:

注意:從API 19開始,所有重複警報都是不精確的。如果您的應用程序需要精確的交付時間,那麼它必須使用一次性精確警報,如上所述每次重新安排時間。其targetSdkVersion早於API 19的傳統應用程序將繼續擁有其所有警報,包括重複警報,並將其視爲確切對待。

您必須將targetSdkVersion設置爲小於19的值,或者使用一次性精確報警並每次重新設置它們。

這是java.util.Timer

Timer timer = new Timer(); 
      timer.scheduleAtFixedRate(new TimerTask() { 

       @Override 
       public void run() { 
        //Put your code here 
        Log.d("Log", "Hello World"); 
       } 
      }, 0, 1000); 
+0

但[官方文檔(http://developer.android.com/reference/android/app/AlarmManager.html)說'的official documentation

簡單的解決方案注意:報警管理器適用於您希望在特定時間運行應用程序代碼的情況,即使您的應用程序當前未運行。所以我使用它,您是否可以顯示一次性精確警報的樣本? – Kathi

+0

如果你只是想要一個簡單的計時器每秒做一些事情,你也可以使用java Timer類。另一個解決方案是處理程序,取決於你真正想要做什麼 – Erich

+0

嗯,謝謝我將使用簡單的處理程序,有沒有機會用報警管理器來實現這一點? – Kathi