从应用程序退出时,Android服务停止 [英] Android service stops when I exit from application

查看:242
本文介绍了从应用程序退出时,Android服务停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有服务的应用程序.服务是在创建方法的主要活动中由应用启动的.我从应用程序退出时出现我的问题:服务停止,有时立即重新启动,有时重新启动.我在4.1.2版和2.3.6版中进行了测试.版本2.3.6中不再提供该服务.我的方法错了吗?在停止时间和重新启动时间之间,必须阻止的呼叫才能到来.退出应用程序后,有什么方法可以不停止服务吗? 注意:我无法使用远程服务.

I have an application with a service. Service is started by app in the main activity on create method. My problem occurs when I exit from application: service stops and sometimes restarts immediately, sometimes restarts lately. I tested in version 4.1.2 and version 2.3.6. service does not again in version 2.3.6. Is my approach wrong? Between the stop and restart times, the call that must blocked can come. Is there any way to not stop the service when app is exited? Note: I cannot use a remote service.

我将startForeground添加到服务中,但存在相同的问题.当我退出应用程序时,该服务停止并且没有重启版本2.3.3,甚至我也看到了notification.but phoneStateListener为空值 如何确保退出应用程序时服务不会停止

i added startForeground to service but the same problem. when i exit application the service stopped and not restart version 2.3.3 even i see notification.But phoneStateListener takes null value How can i ensure when i exit application, the service does not stop

 //application main metod
@Override
protected void onCreate(Bundle savedInstanceState) {              
//.....

    startService(new Intent(getApplicationContext(), MyPhoneStateListener.class));
}




 @Override
 public int onStartCommand(Intent intent, int flags, int startId)
 {
    setCallListener();    
    setNoti();
    return START_STICKY;
 }

 private void setCallListener()
 {
        try
        {
            if (phoneStateListener==null)
            {

              phoneStateListener = new StateListener();
              telephonymanager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
                 telephonymanager.listen(phoneStateListener,PhoneStateListener.LISTEN_CALL_STATE);
            }
        }
        catch(Exception ex)
        { 

        }
    }

private void setNoti() {

private void setNoti() {

    Notification notification= new Notification(R.drawable.ic_launcher,  getResources().getString(R.string.app_name), System.currentTimeMillis());

    Intent main = new Intent(this, MainActivity.class);

    main.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, main,  PendingIntent.FLAG_UPDATE_CURRENT);

    notification.setLatestEventInfo(this, getResources().getString(R.string.app_name), getResources().getStringArray(R.array.blockstate)[Sabitler.ShieldState].toString(), pendingIntent);

    notification.flags |= Notification.FLAG_FOREGROUND_SERVICE | Notification.FLAG_NO_CLEAR;

    startForeground(123456, notification);  
}

我按照Dhawal Sodha的建议将callstatelistener的设置从服务位置更改为广播位置,但在这种情况下,阻止不必要的通话会变慢.我认为在广播中TelephonyManager会初始化每个呼叫.下面的广播代码.当我使用服务时 当退出主要活动时,服务会在2.3.3版中停止.任何的想法 ?广播还是服务?如何使广播更快?每个调用变量和对象都在广播中再次初始化.

i change the where setting callstatelistener from service to broadcast as Dhawal Sodha advised but in this case blocking unwanted call get slow. i think on broadcast TelephonyManager initializes every calling. Broadcast code below. When i use service, when main activity is exited service stopped in version 2.3.3. Any idea ? Broadcast or service ? How can make broadcast faster? every calling variables and object initialize again in broadcast.

public class PhoneStateBroadcastReceiver extends BroadcastReceiver{

    MyCustomStateListener myCustomStateListener;
    TelephonyManager telephonymanager;
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        try
        {
            //Log.e("receiver1",String.valueOf(System.currentTimeMillis()));

            telephonymanager = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
            myCustomStateListener = new MyCustomStateListener(context,telephonymanager);
            telephonymanager.listen(myCustomStateListener, PhoneStateListener.LISTEN_CALL_STATE);
            //Log.e("receiver2",String.valueOf(System.currentTimeMillis()));
            telephonymanager.listen(myCustomStateListener, PhoneStateListener.LISTEN_NONE);
        }
        catch(Exception ex)
        { 
            Toast.makeText(context,"setCallListener:"+ex.toString(), Toast.LENGTH_SHORT).show();
        }
    }   
}

推荐答案

将此代码添加到onStartCommand()方法中

Add this code in your onStartCommand() method

  showToast("onStart");

  Intent intent = new Intent(this, NotStickyActivity.class);
  intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
  PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

  Notification noti = new Notification.Builder(getApplicationContext())
              .setContentTitle("Pratikk")
              .setContentText("Subject")
              .setSmallIcon(R.drawable.ic_launcher)
              .setContentIntent(pendingIntent)
              .build();

  startForeground(1234, noti);     

  return Service.START_STICKY;

它不会停止您的服务.

这篇关于从应用程序退出时,Android服务停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆