Android服务退出离开imp。数据公开

时间:2013-01-06 15:43:04

标签: android android-intent broadcastreceiver android-service intentfilter

我正面临一个严重的问题。在service内部我打开Wifi连接并在任务完成后关闭它。因为,service在任何时候退出,我都会面临连接打开并保持打开的问题。

我有没有办法处理这个问题,因为我正在使用START_STICKY或者我只能以编程方式处理它?

编辑:我可以通过几个接收器(BroadcastReceiver)分享我的意图信息。例如,我将为行动android.net.wifi.wifi_state_changed编写另一个接收器,我现有的接收器用于android.intent.action.PHONE_STATE。 如果可以实现,我可以做点什么。

EDIT2 :我的代码如下:

public class CallReceiver extends BroadcastReceiver {
    private static final String LOG_TAG = "CallReceiver";
    private static final String CALL_ACTION = "android.intent.action.PHONE_STATE";

    @Override
    public void onReceive(Context context, Intent callIntent)
    {
        Log.d(LOG_TAG, "----------------Inside onReceive of CallReceiver----------------");


        if (callIntent.getAction().equals(CALL_ACTION))
        {

            try
            {
                Intent myIntent = new Intent(context, MyService.class);
                context.startService(myIntent);
            }
            catch (Exception e)
            {
                e.printStackTrace();
                Log.d(LOG_TAG,"----------------Exception occured while starting service----------------");
            }
        }
    }
}


public class MyService extends Service {
    private Context context;
    private static final String LOG_TAG = "MyService";
    private Thread thread = null;

    public MyService() 
    {
        super();
        Log.d(LOG_TAG, "----------------Inside Email Service constructor----------------");
    }

    public int onStartCommand(Intent myIntent, int flags, int startId)
    {
        Log.d(LOG_TAG, "----------------Email Service Command Started----------------");
        try
        {
            context = getApplicationContext();
            if(thread == null || !thread.isAlive())
            {
                thread = new Thread(new MyRunnable("Email Sender", myIntent));
                thread.start();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            Log.d(LOG_TAG,
                  "----------------Exception occured in Email Service onStartCommand----------------");
        }
        return START_REDELIVER_INTENT;
    }

    class MyRunnable implements Runnable {
        String name;
        Intent myIntent;

        public MyRunnable(String name, Intent myIntent) {
            this.name = name;
            this.myIntent = myIntent;
        }

        @Override
        public void run()
        {
            try
            {
                doStuff(emailIntent);
            }
            catch (NumberFormatException e)
            {
                e.printStackTrace();
                Log.d(LOG_TAG, e.getMessage());
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
                Log.d(LOG_TAG, e.getMessage());
            }
            catch (Exception e)
            {
                e.printStackTrace();
                Log.d(LOG_TAG, e.getMessage());
            }
            finally
            {
                stopSelf();
            }
        }
    }

    private void doStuff(Intent emailIntent) throws InterruptedException, Exception
    {
        if (context != null)
        {
            boolean isWifiConnection = false;

            try
            {
                // Check if WiFi connection is available ,if yes try opening it;
                // Attempt to open WiFi connection
                isWifiConnection = Utility.isEnableWifiSuccessful(getApplicationContext());
                Log.d(LOG_TAG, "----------------Wifi conn enabled = " + isWifiConnection
                               + "----------------");

                if (isWifiConnection)
                {
                    // Do more stuff
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
                throw e;
            }
            finally
            {
                // Code never reaches here !! Somehow, the service stops and by
                // the time the service stops,
                // WiFi has been enabled
                try
                {
                    if (isWifiConnection)
                    {
                        Utility.isDisableWifiSuccessful(getApplicationContext());
                    }
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                    Log.d(LOG_TAG,
                          "----------------Error occured while closing network connections----------------");
                }
            }
        }
        else
        {
            Log.d(LOG_TAG, "----------------Context is null----------------");
        }
    }

    @Override
    public IBinder onBind(Intent intent)
    {
        // TODO Auto-generated method stub
        return null;
    }
}

现在,如果我有另一个接收器作为NetworkReceiver

public class NetworkReceiver extends BroadcastReceiver {

    private static final String ACTION = "android.net.wifi.WIFI_STATE_CHANGED";
    private static final String LOG_TAG = "NetworkReceiver";

    @Override
    public void onReceive(Context context, Intent networkIntent)
    {
        if(networkIntent.getAction().equals(ACTION))
        {
            Log.d(LOG_TAG, "----------------Inside Network Receiver----------------");
            //Do something which will keep track who has opened the WiFi connection
        }
    }
}

然后myIntentnetworkIntent可以共享信息,MySerivce可以阅读该信息。

任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:0)

当内存太低时服务退出,因为您已经在使用START_STICKY,一旦内存资源可用,服务将重新启动。我相信您可能需要检查连接是否已打开并且您已完成任务,然后使用stopSelf()停止服务。

希望这有帮助。

谢谢, 拉梅什