我想每10秒重新啓動我的IntentService(它處理HTTP POST請求)。我嘗試使用AlarmManager和PendingIntent作爲每個帖子中描述的。但我的IntentService不啓動。我無法找到任何理由,所以任何幫助,將不勝感激。帶警報管理器的IntentService
IntentService
public class MyService extends IntentService{
public MyService() {
super("MyService");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Service Started", Toast.LENGTH_SHORT).show();
System.out.println("Service Started");
// POST request code here
}
}
MainActivity
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start();
}
public void start() {
Intent intent = new Intent(this, MyService.class);
intent.putExtra("com.hybris.proxi.triggerTime", 5000);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
long trigger = System.currentTimeMillis() + (5*1000);
AlarmManager am =(AlarmManager)getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, trigger, pendingIntent);
}
}
@MikeM 。是的,我註冊了像「<服務android:name =」。MyService「/>' –
@MikeM。沒有android:minSdkVersion =「11」 –