如何使用警报管理重复我的服务? [英] How to repeat my service using alarm manager?
本文介绍了如何使用警报管理重复我的服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经做了一个基于位置的应用程序。现在我需要在30分钟运行一次,我的位置服务。对于我所使用警报管理尝试,但它运行我的服务只一次。我需要它每30分钟跑?
我的code:
gps_back_process.java:
公共类gps_back_process延伸服务
{
私有静态最后弦乐TAG =为MyService
@覆盖
公共无效的onCreate(){
// TODO自动生成方法存根
super.onCreate();
Log.d(测试,服务创造得到了);
Toast.makeText(这一点,gps_back_process,的onCreate();,Toast.LENGTH_LONG).show();
} @覆盖
公众的IBinder onBind(意向为arg0){
// TODO自动生成方法存根
返回null;
} @覆盖
公共无效的onDestroy(){
// TODO自动生成方法存根
super.onDestroy();
}
@覆盖
公共无效调用onStart(意向意图,诠释startid)
{
意向意图=新意图(getBaseContext(),gps.class);
intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(意向);
Toast.makeText(这一点,gps_back_process.onCreate();,Toast.LENGTH_LONG).show();
Log.d(TAG,在onStart);
}
}
gps.java:
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main); {
日历cur_cal = Calendar.getInstance();
cur_cal.setTimeInMillis(System.currentTimeMillis的());
cur_cal.add(Calendar.SECOND,90);
Log.d(测试,日历设置时间:+ cur_cal.getTime());
意向意图=新意图(gps.this,gps_back_process.class);
Log.d(测试,意图创造);
PI的PendingIntent = PendingIntent.getService(gps.this,0,意向,0);
AlarmManager alarm_manager =(AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm_manager.set(AlarmManager.RTC,cur_cal.getTimeInMillis(),PI);
Log.d(测试,报警管理器中设置);
Toast.makeText(这一点,gps_back_process.onCreate(),
Toast.LENGTH_LONG).show();
}
解决方案
您需要调用方法 setRepeating()
在 AlarmManager
引用而不是set()方法。
AlarmManager alarm_manager =(AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm_manager.setRepeating(AlarmManager.RTC,cur_cal.getTimeInMillis(),180000,PI);
I have done a location based app. Now I need to run my location service once in 30 mins. For that I have tried using alarm manager but it is running my service only once. I need it to run every 30 mins?
My code:
gps_back_process.java:
public class gps_back_process extends Service
{
private static final String TAG = "MyService";
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.d("Testing", "Service got created");
Toast.makeText(this, "gps_back_process,onCreate();", Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startid)
{
Intent intents = new Intent(getBaseContext(),gps.class);
intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intents);
Toast.makeText(this, "gps_back_process.onCreate();", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
}
}
gps.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
{
Calendar cur_cal = Calendar.getInstance();
cur_cal.setTimeInMillis(System.currentTimeMillis());
cur_cal.add(Calendar.SECOND, 90);
Log.d("Testing", "Calender Set time:" + cur_cal.getTime());
Intent intent = new Intent(gps.this, gps_back_process.class);
Log.d("Testing", "Intent created");
PendingIntent pi = PendingIntent.getService(gps.this, 0, intent, 0);
AlarmManager alarm_manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm_manager.set(AlarmManager.RTC, cur_cal.getTimeInMillis(), pi);
Log.d("Testing", "alarm manager set");
Toast.makeText(this, "gps_back_process.onCreate()",
Toast.LENGTH_LONG).show();
}
解决方案
You need to call the method setRepeating()
on the AlarmManager
reference rather than set() method.
AlarmManager alarm_manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm_manager.setRepeating(AlarmManager.RTC, cur_cal.getTimeInMillis(), 180000, pi);
这篇关于如何使用警报管理重复我的服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文