0

私は重大な問題に直面しています。内部では、serviceWifi 接続を開き、タスクの完了後に閉じています。そのため、service任意の時点で終了するため、接続が開いたままになるという問題に直面します。

私が使用しているようにこれを処理できる方法はありSTART_STICKYますか、それともプログラムでのみ処理する必要がありますか?

EDIT : インテント情報を複数のレシーバー ( 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その情報を読み取ることができます。

どんな助けでも本当に感謝しています。

4

1 に答える 1

0

すでに START_STICKY を使用しているため、メモリが不足するとサービスが終了します。メモリ リソースが利用可能になるとサービスが再開されます。接続が開いているかどうかを確認する必要があり、タスクが完了したら、stopSelf() を使用してサービスを停止する必要があると思います。

お役に立てれば。

ありがとう、ラメッシュ

于 2013-01-06T15:50:52.553 に答える