Android的服务和AlarmManager不停止

编程入门 行业动态 更新时间:2024-10-09 15:15:08
本文介绍了Android的服务和AlarmManager不停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好我在我的活动名称创建一个服务,其中滑动地图GPS指点也乳宁。我的服务是定期检查(使用AlarmManager)使用JSON从服务器的响应,如果响应是真实的新活动开始。我的新活动仅30秒,当我的新的活动启动的服务应停止并重新开始。我的要求是服务应该运行时我的应用程序是不是在前台(意思是,如果我忙于其他应用服务应该正在运行)。我的问题是,当反应成为现实的新活动启动,但服务没有停止(基本上AlarmManager不断呼叫服务)和新活动是一次又一次的开始。关于暂停()方法,我滑的活动我停止AlarmManager它的工作原理我只有在应用上的前景,但如果我主页上点击链接或打开任务栏的服务我的服务停止的任何事情。有没有什么办法阻止AlarmManager在新的活动?所以,当我的新活动开放的服务应该停止。(我的基本任务是新的活动打开时停止服务)或任何其他解决方案?

Hello everyone i am creating a service in my Activity name sliding where map with GPS pointing is also runing. my service is periodically checking(Using AlarmManager) response from server using JSON, if response is true new activity starts. My new activity is only for 30 sec and when my new Activity starts service should stop and again starts. My requirement is service should be running while my app is not on foreground( mean if I am busy with other apps the service should be running). My Problem is that when response comes true new activity starts but service is not stopping(Basically AlarmManager is continuously calling service) and new Activity is starting again and again. On my sliding activity on the Pause() method i stop AlarmManager it works me only if app is on foreground but if i click on Home button or open any thing from task bar service my service stopped. Is there any way to stop AlarmManager in new Activity?? so that when my new Activity open service should stop.(my basic task is to stop service when new activity opens) or any other solution??

这是我的java code onStartCommand()方法,其中服务与服务器进行通信

here is my java code onStartCommand() method where service is communicating with server

public int onStartCommand(Intent intent, final int flags, int startId) { try { if (json.getString(KEY_SUCCESS) != null) { String res = json.getString(KEY_SUCCESS); if(Integer.parseInt(res) == 1){ Intent call = new Intent(CallService.this, CallScreen.class); call.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(call); }}} catch (JSONException e) { e.printStackTrace(); } return START_NOT_STICKY; }

主要活动

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sliding); Calendar cal = Calendar.getInstance(); cal.add(Calendar.SECOND, 40); Intent intent = new Intent(this, service.class); // Add extras to the bundle intent.putExtra("foo", "bar"); pintent = PendingIntent.getService(this, 0, intent, 0); alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE); int i; i=15; alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), i* 1000, pintent); startService(intent); }

和这里是我在活动启动服务

and here is my activity starting in service

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.call); }

现在,当服务启动呼叫活动的服务应该停止并AlarmManager也停下来调用服务

Now when service start call activity service should stop and AlarmManager also stop to calling the service

推荐答案

这里是接收器的服务:

public class ApplicationMine extends Service{ public static final String ACTION_STOP_SERVICE = "com.package.name.ACTION_STOP_SERVICE"; public void log(String whatever){ Log.e(" Service state : ", whatever); } @Override public void onCreate() { log("onCreate"); registerReceiver(stopBroadcastReceiver, new IntentFilter(ACTION_STOP_SERVICE)); //register your receiver here. This will keep looking for the ACTION_STOP_SERVICE action. } protected BroadcastReceiver stopBroadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { log("onReceive"); stopSelf(); // or stopService(name); } }; public void onDestroy() { log("onDestroy"); unregisterReceiver(stopBroadcastReceiver); super.onDestroy(); } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return null; } }

现在你可以有象下面这样BaseActivity:

public class BaseActivity extends Activity { public void log(String whatever){ Log.e(" CCCCCCC state: ", whatever); } public void stopUpdates( String action) { Log.d("", "A.R. stopLocationUpdate()"); // let's broadcast location data to any activity waiting for updates Intent intent = new Intent(action); sendBroadcast(intent); } }

现在在这里不用你等活动

Now here goes your other activities

public class B extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_b); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); stopUpdates(ApplicationMine.ACTION_STOP_SERVICE); } }

原谅我,如果任何的最佳方法忽略这里。尽管如此,我希望你是好去。请让我知道,如果它帮助。

Excuse me if any optimal approach misses here. Still I hope you are good to go. Please let me know if it helps.

更多推荐

Android的服务和AlarmManager不停止

本文发布于:2023-11-27 11:04:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637808.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Android   AlarmManager

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!