データをダウンロードするためのインテントサービスを作成し、ブロードキャスト受信とアラームマネージャーで繰り返したいのですが、インテントサービスを呼び出すにはどうすればよいですか?これを試しましたが、機能しません...
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Don't panik but your time is up!!!!.",
Toast.LENGTH_LONG).show();
// Vibrate the mobile phone
Vibrator vibrator = (Vibrator)
context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(500);
intent.setData(Uri.parse("http://www.mysite.net/LEDstate.txt"));
intent.putExtra("urlpath", "http://www.mysite.net/LEDstate.txt");
//startService(intent);
context.startService(new Intent(context, DownloadService.class));
}
パブリッククラスAlarmActivityはActivity{を拡張します
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startAlert(View view) {
EditText text = (EditText) findViewById(R.id.time);
int i = Integer.parseInt(text.getText().toString());
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
//alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
// + (i * 1000),pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
,(i*1000),pendingIntent);
// alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar
// .getTimeInMillis(), intervals, pendingIntent);
Toast.makeText(this, "Alarm set in " + i + " seconds",
Toast.LENGTH_LONG).show();
}