[Java for Android App Development] student71:
I wrote an app that unfortunately doesn't start at boot. I am also aware (theoretically) of the change in policy of BroadcastReceiver from 3.1 version. But I could not catch what the point is, how I register the service from user activity?

public class AlarmScheduleActivity extends Activity {

// UI parameters

Button btnStart;

Button btnStop;

[MENTION=439709]override[/MENTION]

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

btnStart = (Button)findViewById(R.id.button1);

btnStop = (Button)findViewById(R.id.button2);

}

public void btnStartSchedule(View v) {

try {

AlarmManager alarms = (AlarmManager) this

.getSystemService(Context.ALARM_SERVICE);

Intent intent = new Intent(getApplicationContext(),

AlarmReceiver.class);

intent.putExtra(AlarmReceiver.ACTION_ALARM,

AlarmReceiver.ACTION_ALARM);

final PendingIntent pIntent = PendingIntent.getBroadcast(this,

1234567, intent, PendingIntent.FLAG_UPDATE_CURRENT);

alarms.setRepeating(AlarmManager.RTC_WAKEUP,

System.currentTimeMillis(), 2000, pIntent);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public void btnCancelSchedules(View v) {

Intent intent = new Intent(getApplicationContext(),

AlarmReceiver.class);

intent.putExtra(AlarmReceiver.ACTION_ALARM,

AlarmReceiver.ACTION_ALARM);

final PendingIntent pIntent = PendingIntent.getBroadcast(this, 1234567,

intent, PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager alarms = (AlarmManager) this

.getSystemService(Context.ALARM_SERVICE);

alarms.cancel(pIntent);

}

}

public class AlarmReceiver extends BroadcastReceiver {

public static String ACTION_ALARM = "com.alarammanager.alaram";

[MENTION=439709]override[/MENTION]

public void onReceive(Context context, Intent intent) {

Bundle bundle = intent.getExtras();

String action = bundle.getString(ACTION_ALARM);

if (action.equals(ACTION_ALARM)) {

Log.i("Alarm Receiver", "If loop");

Intent inService = new Intent(context,TaskService.class);

context.startService(inService);

}

}

}

public class BootBroadcastReceiver extends BroadcastReceiver {
[MENTION=439709]override[/MENTION]

public void onReceive(Context context, Intent intent) {

Intent alarmIntent = new Intent("com.company.android.AlarmReceiver");

PendingIntent pi = PendingIntent.getBroadcast(context, 0, alarmIntent,

PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis(),

2000, pi);

}

}

public class TaskService extends IntentService {

public TaskService() {

super("TaskService");

// TODO Auto-generated constructor stub

}
[MENTION=439709]override[/MENTION]

protected void onHandleIntent(Intent arg0) {

// Do some task

Log.i("TaskService", "Service running: yes-yes-yes");

}

}

AndroidManifest.xml

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<application

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

<activity

android:name=".AlarmScheduleActivity"

android:label="@string/app_name" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<receiver android:name=".BootBroadcastReceiver"

android:enabled="true" >

<intent-filter>

<action android:name="android.intent.action.BOOT_COMPLETED" />

<category android:name="android.intent.category.DEFAULT" />

</intent-filter>

</receiver>

<receiver

android:name="AlarmReceiver"

android:process=":remote" >

</receiver>

<service android:name=".TaskService" >

</service>

</application>