Service、Alarm Manager 和 Broadcast Receiver 之间的关系
Relationship among Service, Alarm Manager and Broadcast Receiver
我是 Android 开发的新手,如果有人能用一些示例代码向我解释服务、警报管理器和广播接收器之间的关系,我将不胜感激。我正在开发一个 Android 应用程序,我需要每 10 分钟执行一次代码的特定部分。此外,我想确保我的应用程序不会被 Android OS 杀死,如果它由于某些原因被杀死,例如 phone 运行 低 space,那么我的应用程序应该重新启动(当然是从用户离开的同一个地方)。此外,当 phone 重新启动时,应用程序应该会再次启动。
如有任何帮助,我们将不胜感激。谢谢。
这是设置闹钟的完整代码
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);
}
现在接收器 Class
public class MyReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Do Your Stuff here
}
还有 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>
我是 Android 开发的新手,如果有人能用一些示例代码向我解释服务、警报管理器和广播接收器之间的关系,我将不胜感激。我正在开发一个 Android 应用程序,我需要每 10 分钟执行一次代码的特定部分。此外,我想确保我的应用程序不会被 Android OS 杀死,如果它由于某些原因被杀死,例如 phone 运行 低 space,那么我的应用程序应该重新启动(当然是从用户离开的同一个地方)。此外,当 phone 重新启动时,应用程序应该会再次启动。
如有任何帮助,我们将不胜感激。谢谢。
这是设置闹钟的完整代码
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);
}
现在接收器 Class
public class MyReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Do Your Stuff here
}
还有 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>