我从应用程序退出时Android服务停止

use*_*641 6 service android android-service

我有一个服务的应用程序.服务由app在create method的主要活动中启动.我退出应用程序时出现问题:服务停止,有时立即重启,有时最近重启.我在4.1.2版和2.3.6版中测试过.版本2.3.6中不再提供服务.我的方法有误吗?在停止和重新启动时间之间,必须阻止的呼叫可以到来.退出应用程序时有没有办法停止服务?注意:我无法使用远程服务.

我添加了startForeground服务,但同样的问题.当我退出应用程序服务停止而不重启版本2.3.3甚至我看到notification.But phoneStateListener取空值我怎样才能确保当我退出应用程序时,服务不会停止

 //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)
        { 

        }
    }
Run Code Online (Sandbox Code Playgroud)

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);  
}
Run Code Online (Sandbox Code Playgroud)

我改变了设置callstatelistener从服务到广播的地方,正如Dhawal Sodha所建议的那样,但在这种情况下阻止不需要的呼叫变得缓慢.我想在广播TelephonyManager初始化每个调用.以下广播代码.当我使用服务时,当主要活动退出时,服务在版本2.3.3中停止.任何的想法 ?广播还是服务?如何让播放速度更快?每个调用变量和对象在广播中再次初始化.

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();
        }
    }   
}
Run Code Online (Sandbox Code Playgroud)

Sir*_*get 7

您是否正在启动服务startService?如果是这样,当您结束活动时,该服务不受任何限制,可以由系统停止.

如果要运行后台服务,无论您的活动如何,请使用startForeground并提供持续通知.

请参阅服务参考:http://developer.android.com/reference/android/app/Service.html

UPDATE

完成你的应用程序System.exit(0)?当然,您的服务会停止,这会终止您的流程.你永远不应该退出这样的应用程序 - 只是finish()你的活动.


小智 7

在onStartCommand()方法中添加此代码

  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;
Run Code Online (Sandbox Code Playgroud)

它不会停止您的服务.