2017-10-01 8 views
0

私はバックグラウンドでデータトラック用のアプリケーションをプログラミングしたいので、サービスを利用します。 デバイスが送受信するすべてのデータを監視するプログラムを作成し、受信または受信したメッセージの合計量が指定された値に達すると、インターネットデバイスがオフになります。破棄されたアクティビティから再開されたサービスにnullを送信しないようにする

だから私は、データを監視するために、次のコードを使用:

mStartRX = TrafficStats.getTotalRxBytes(); 
mStartTX = TrafficStats.getTotalTxBytes(); 

そして私は、バックグラウンドでバックグラウンドで動作するサービスを使用。

edittextを使用しているユーザーからダウンロードまたはアップロードの上限を指定するには、私はmainactivityでこの制限を要求し、この値をサービスに送信します。

問題は次の場合です。プログラムを破棄すると、サービスを再起動してNULL値を取得し、プログラムがクラッシュします。

私のアプリケーションのコード:

主な活動:

package ir.alexandre9009.service; 


import android.app.AlertDialog; 
import android.content.Context; 
import android.net.TrafficStats; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Handler; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class MainActivity extends Activity { 
    public static Handler mHandler = new Handler(); 
public static long UPP; 
public static long DLL; 
    Button startService,stopService; 
    public Context context=this; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     if (FirstService.mStartRX == TrafficStats.UNSUPPORTED || FirstService.mStartTX == TrafficStats.UNSUPPORTED) { 
      AlertDialog.Builder alert = new AlertDialog.Builder(this); 
      alert.setTitle("Uh Oh!"); 
      alert.setMessage("Your device does not support traffic stat monitoring."); 
      alert.show(); 
     } else { 
      mHandler.postDelayed(mRunnable, 1000); 
     } 
     startService=(Button)findViewById(R.id.startService); 
     stopService=(Button)findViewById(R.id.stopService); 

     startService.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       EditText UP = (EditText) findViewById(R.id.UP); 
       String UPPP = UP.getText().toString(); 
       UPP=Long.parseLong(UPPP); 

       EditText DL = (EditText) findViewById(R.id.DL); 
       String DLLL = DL.getText().toString(); 
       DLL=Long.parseLong(DLLL); 
       Intent intent = new Intent(getApplicationContext(), FirstService.class); 
       String myString = DLLL; 
       intent.putExtra("StringName", myString); 
       startService(intent); 
      } 

     }); 

     stopService.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       stopService(new Intent(getBaseContext(),FirstService.class)); 
      } 
     }); 
    } 
    public final Runnable mRunnable = new Runnable() { 
     public void run() { 
      TextView RX = (TextView)findViewById(R.id.RX); 
      TextView TX = (TextView)findViewById(R.id.TX); 
      RX.setText(Long.toString(FirstService.rxBytes)); 
      TX.setText(Long.toString(FirstService.txBytes)); 
      mHandler.postDelayed(mRunnable, 1000); 
     } 
    }; 
} 

サービス:

package ir.alexandre9009.service; 

import android.app.AlertDialog; 
import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
import android.net.TrafficStats; 
import android.net.wifi.WifiManager; 
import android.os.Handler; 
import android.os.IBinder; 
import android.widget.TextView; 
import android.widget.Toast; 

public class FirstService extends Service{ 
    public static long mStartRX ; 
    public static long mStartTX ; 
    public static long rxBytes ; 
    public static long txBytes ; 
    public long dl=MainActivity.DLL; 
    Context context=this; 
    private final Runnable mRunnable = new Runnable() { 
     public void run() { 

      rxBytes = (TrafficStats.getTotalRxBytes()- mStartRX)/1048576; 
      txBytes = (TrafficStats.getTotalTxBytes()- mStartTX)/1048576; 
      if (rxBytes==2) { 
       stopService(new Intent(getBaseContext(),FirstService.class)); 
       Intent i = new Intent(context,MainActivity.class); 
       context.startActivity(i); 

       // WifiManager wifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE); 
       // wifiManager.setWifiEnabled(false); 
//معرفی توست برای نمایش یک پیام کوتاه به کاربر در هنگام خاموش کردن وای فای 
       Toast.makeText(FirstService.this, "هشدار", Toast.LENGTH_LONG).show(); 

      } 
      mHandler.postDelayed(mRunnable, 1000); 
     } 
    }; 
    private Handler mHandler = new Handler(); 

    @Override 
    public IBinder onBind(Intent arg0) { 
     return null; 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 

     Toast.makeText(this,"staart",Toast.LENGTH_LONG).show(); 
     mStartTX=0; 
     mStartRX=0; 
     mStartRX = TrafficStats.getTotalRxBytes(); 
     mStartTX = TrafficStats.getTotalTxBytes(); 
     mHandler.postDelayed(mRunnable, 1000); 

     return Service.START_STICKY; 


    } 

    @Override 
    public void onDestroy() { 
     TrafficStats.clearThreadStatsTag(); 
     Toast.makeText(this,"FirstService Stoped",Toast.LENGTH_LONG).show(); 
     mStartTX=0; 
     mStartRX=0; 
     super.onDestroy(); 
    } 



} 

AndroidManifest:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="ir.alexandre9009.service"> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/> 
    <uses-permission android:name="android.permission.UPDATE_DEVICE_STATS" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 
    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:roundIcon="@mipmap/ic_launcher_round" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <service android:name=".FirstService" 

      > 

     </service> 
    </application> 

</manifest> 

私を助けてください...

+0

助けてください... –

+0

より良いコードを入力してください。少なくともスタックトレースを提供 –

+0

@BardyaMomeni سوالدقیقمتویآخرینپستنوشتم: http://p30droid.com/topic/6911-%D9%85%D8%A7%D9%86%DB%8C%D8%AA D8%A8%D8%AA%D8%A7%DB%8C-%D8%A7%D8%D8%D8%D8% D8%A8%D8%A7%D8%D8%AA%DB%B1%D8%B1%D8%B3%D8%A7%D9%84%DB%8C-%D9%88% 8C-%D8%A7%DB%8C%D9%86%D8%AA%D8%B1%D9%86%D8%AA /?do = findComment&comment = 26861 –

答えて

0

問題がこの行にあります

public long dl = MainActivity.DLL;

アクティビティの変数を参照していますが、アクティビティが破棄されたときにこの変数が有効でなくなったため、Null例外が発生します。

この値はインテントを使用して取得する必要があります。

もう1つの問題は、ユーザーに通知する必要があるフォアグラウンドサービスを除いて、システムによってシステムが強制終了されないようにすることができないことです。しかし、これはあなたの状況には適していません。

したがって、インテントがnullであるかどうかを確認するのが最善の方法です。 nullでない場合は、値を取得してPreferencesまたはDatabaseなどに保存します。値がnullの場合は、以前に格納した値を取得します。

関連する問題