2012-12-07 30 views
3

我为下载数据的目的服务,我要与广播重复接收和报警manager.How我打电话给我的目的服务?我试试这个,但没有工作....如何从广播接收器调用IntentService?

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扩展活动{

@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(); 
    } 
+0

你在这个类的'DownloadService'的清单中定义了什么? –

+0

是清单是好的。我尝试转换vogella教程intentservice每x秒重播。 – antkan

+0

你没试过@Zozokk的解决方案吗?它对我来说很好。 –

回答

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); 
+0

我尝试但没有工作...我认为需要修改活动 – antkan

+0

只工作一次.... – antkan

0

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

0

当您的brodcast获得收到时,您的广播活动将被调用。

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

看到下面的代码:

@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为被叫活动 这是你必须做什么,如果你要拨打的活动,而该消息得到广播。那时你可以根据需要启动相应的服务。

希望你明白我的观点。

+0

你是对的。我是新的android开发。 如何将此转换为对我有效的工作?我将不胜感激您的帮助... – antkan

+0

@antkan:我认为您应该引用此内容:http://www.vogella.com/articles/AndroidNotifications/article.html –

0

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

0

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