antkan
antkan

Reputation: 61

How i call IntentService from Broadcast Receiver?

I make an intent service for download data and i want to repeat with broadcast receive and alarm manager.How i call my intent service?I try this but no work....

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

        }

public class AlarmActivity extends 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();
  }

Upvotes: 5

Views: 11971

Answers (4)

Malti Devnani
Malti Devnani

Reputation: 104

You did not call startAlert method in onCreate function.

Upvotes: 0

Shreyash Mahajan
Shreyash Mahajan

Reputation: 23596

When your brodcast getting received, your broadcast activity will called.

at that time you can call any activity as you want.

see below code:

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

In above code, AlarmReceiverNotificationForEveryMonth.class is the called activity this is what you have to do if you want to call the activity while the message get broadcast. at that time you can start the respective service as you want.

Hope you got My point.

Upvotes: 0

sujith
sujith

Reputation: 2421

In your code you are not passing data, extras to your Service. You are adding adding data and extras to an Intent object, passing a different Intent object to startService() method. Use the solution suggested by @Zzokk in the other post, it should work.

Upvotes: 1

k_shil
k_shil

Reputation: 2148

Try this:

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

Upvotes: 3

Related Questions