0

サービスを利用したアプリを開発しています。電話がスリープモードのときにサービスを開始および停止したい。次のコードを使用していますが、機能しません。

私のステータスチェッカーサービスコードは

public void onStart(Intent intent, int startd) {

Log.v("check","Services is running");
Log.v("spinner Value","Services is running");
  handler.removeCallbacks(sendNetworkStatus);
    handler.postDelayed(sendNetworkStatus, 1000);
     dataManager = (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);
     wifimanager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
      boolean screenOn = intent.getBooleanExtra("screen_state", false);
      if (!screenOn) {
          // YOUR CODE
          Log.v("Screen","Screen is on>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
         /* if(!isMyServiceRunning())
          {
              startService(intent);  
          }*/    
      } else {
          // YOUR CODE
        // stopService(intent);
          Log.v("Screen","Screen is off>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
      }
}



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

private Runnable sendNetworkStatus = new Runnable() {
    public void run() {
        sendNetworkInfo();
        handler.postDelayed(this, 5000);
         checkstrength(wifiStatus,mobileStatus);
         messageshow(response);
         Log.v("check","ping the URL again and again");
    }
};
/*@Override
public boolean onUnbind(Intent intent) {
    handler.removeCallbacks(sendNetworkStatus);
    handler = null;
    return super.onUnbind(intent);
}*/
private void sendNetworkInfo() {
    ConnectivityManager connection = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo         activeNetwork       = connection.getActiveNetworkInfo();

    if (null != activeNetwork) {
        if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
        {//wifiStatus = "Wifi Status: UP";

         wifiStatus= getWifiSignalStrengthIndBm();
         telephonyManager.listen(phoneStateListener,      
                    PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);

        }
        else{
            wifiStatus=0;
        }

         if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
            {//mobileStatus = "Mobile Status: UP";
             message="mobile";
            // filter.addAction("mobile");
            telephonyManager.listen(phoneStateListener,      
            PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);

            }
         else{
             //mobileStatus=0;
             telephonyManager.listen(phoneStateListener,      
                        PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
         }
         try {
                response = Network.ping(url);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

    } 
    else{

        mobileStatus = 0;
        wifiStatus = 0;
        response="-1";
    }

    intent.putExtra("status", response);
    sendBroadcast(intent);
}
 public int getWifiSignalStrengthIndBm() {
        WifiInfo wifiInfo = wifimanager.getConnectionInfo();

        return wifiInfo.getRssi();
    }
 public void checkstrength(int wifi,int mobile)
    {

        if(wifi!=0 && mobile!=0)
        {

           if(wifi<mobile){
              response="-2";

              wifimanager.setWifiEnabled(false);
              InternetControl.EnableInternet(getApplicationContext());

                          }
        }
        else if(wifi==0&&mobile<=-111)
        {
             response="-3";
            message="signal Strength is too weak";
        }
    }


@Override
public void onDestroy() {
    handler.removeCallbacks(sendNetworkStatus);
     Thread.currentThread().interrupt();
    unregisterReceiver(mReceiver);
    Toast.makeText(getBaseContext(), "service is destroy", Toast.LENGTH_LONG).show();
    super.onDestroy();
}

猫のエラーをログに記録します

09-04 12:46:33.249: E/AndroidRuntime(4177): FATAL EXCEPTION: main
09-04 12:46:33.249: E/AndroidRuntime(4177): java.lang.RuntimeException: Unable to start service com.example.data_connection_monitor.StatusChecker@415923f8 with null: java.lang.NullPointerException
09-04 12:46:33.249: E/AndroidRuntime(4177):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2390)
09-04 12:46:33.249: E/AndroidRuntime(4177):     at android.app.ActivityThread.access$1900(ActivityThread.java:128)
09-04 12:46:33.249: E/AndroidRuntime(4177):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1224)
09-04 12:46:33.249: E/AndroidRuntime(4177):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 12:46:33.249: E/AndroidRuntime(4177):     at android.os.Looper.loop(Looper.java:137)
09-04 12:46:33.249: E/AndroidRuntime(4177):     at android.app.ActivityThread.main(ActivityThread.java:4517)
09-04 12:46:33.249: E/AndroidRuntime(4177):     at java.lang.reflect.Method.invokeNative(Native Method)
09-04 12:46:33.249: E/AndroidRuntime(4177):     at java.lang.reflect.Method.invoke(Method.java:511)
09-04 12:46:33.249: E/AndroidRuntime(4177):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
09-04 12:46:33.249: E/AndroidRuntime(4177):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
09-04 12:46:33.249: E/AndroidRuntime(4177):     at dalvik.system.NativeStart.main(Native Method)
09-04 12:46:33.249: E/AndroidRuntime(4177): Caused by: java.lang.NullPointerException
09-04 12:46:33.249: E/AndroidRuntime(4177):     at com.example.data_connection_monitor.StatusChecker.onStart(StatusChecker.java:77)
09-04 12:46:33.249: E/AndroidRuntime(4177):     at android.app.Service.onStartCommand(Service.java:438)
09-04 12:46:33.249: E/AndroidRuntime(4177):     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2373)
09-04 12:46:33.249: E/AndroidRuntime(4177):     ... 10 more

私を助けてください。私はアンドロイドが初めてです。前もって感謝します!!!!!!!!

4

0 に答える 0