1

I am new to Android Development and would highly appreciate if someone could please explain me the relationship among the Service, Alarm Manager and Broadcast Receiver with some sample code. I am working on an Android App where I need to execute certain part of a code every 10 minute. Also, I want to make sure that my App doesn't get killed by the Android OS and if it is killed due to some reason like phone running low on space, then my App should start again(of course from the same place where the user left). Also, when the phone is restarted, the App should start again.

Any help would be highly appreciated. Thank you.

User11012
  • 107
  • 8

1 Answers1

0

Here Is The Full Code To Set AN Alarm

    public class Alarmset extends Activity implements OnClickListener{
Button set;
TimePicker timepicker;


@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alarmset);
        set=(Button) findViewById(R.id.set);

        set.setOnClickListener(this);
        timepicker = (TimePicker)findViewById(R.id.timepicker);
    }



@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v==set)
        {
            Calendar c = Calendar.getInstance();
            int y = c.get(Calendar.YEAR);
            int m = c.get(Calendar.MONTH);
            int d = c.get(Calendar.DAY_OF_MONTH);

            c.set(y,m,d,timepicker.getCurrentHour(),timepicker.getCurrentMinute(),00);
            setAlarm(c);
        }

private void setAlarm(Calendar targetCal){

          Intent intent = new Intent(getBaseContext(), My.class);


          PendingIntent pendingIntent = PendingIntent.getBroadcast(Alarmset.this, 0, intent, 0);
          AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
          alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);

         }

Now Reciever Class

public class MyReciever extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Do Your Stuff here

    }

And the Xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.mainmenu.Alarmset"
     >

    <Button
        android:id="@+id/set"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/timepicker"
        android:layout_below="@+id/timepicker"
        android:layout_marginTop="62dp"
        android:text="Set"
        android:textStyle="bold" />

    <TimePicker
        android:id="@+id/timepicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />
</RelativeLayout>
Quick learner
  • 10,632
  • 4
  • 45
  • 55