4

我为下载数据创建了一个意图服务,我想重复广播接收和警报管理器。我如何调用我的意图服务?我尝试了这个但没有工作....

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();
  }
4

4 回答 4

3

尝试这个:

Intent newIntent = new Intent(context, DownloadService.class)
newIntent.setData(Uri.parse("http://www.mysite.net/LEDstate.txt"));
newIntent.putExtra("urlpath", "http://www.mysite.net/LEDstate.txt");


context.startService(newIntent);
于 2012-12-07T12:44:37.040 回答
0

在您的代码中,您没有将数据传递给您的Service. 您正在向对象添加数据和附加功能Intent,将不同的Intent对象传递给startService()方法。使用@Zzokk 在另一篇文章中建议的解决方案,它应该可以工作。

于 2012-12-07T12:49:19.830 回答
0

当您的广播被接收时,您的广播活动将被调用。

那时您可以根据需要调用任何活动。

见下面的代码:

 @Override
    public void onReceive(Context context, Intent intent) {

        AM_EM_APRIL_2012 = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
                Intent in1 = new Intent(context, AlarmReceiverNotificationForEveryMonth.class);
                in1.putExtra("MyMessage","PAYE payment and employer monthly schedule for March");
                PI_EM_APRIL_2012 = PendingIntent.getBroadcast(context, 1, in1, PendingIntent.FLAG_UPDATE_CURRENT);
                AM_EM_APRIL_2012.set(AlarmManager.RTC_WAKEUP, calendar_PAYE_20_APRIL_2012.getTimeInMillis(),PI_EM_APRIL_2012);
            }

在上面的代码中,AlarmReceiverNotificationForEveryMonth.class 是被调用的活动,如果你想在消息被广播时调用活动,这是你必须做的。那时您可以根据需要启动相应的服务。

希望你明白我的意思。

于 2012-12-07T12:50:57.507 回答
0

您没有在函数中调用startAlert方法。onCreate

于 2017-05-03T05:25:28.770 回答