2016-07-23 68 views
1

einrichten ich bin eine Anwendung erstellen, die die Anzahl der Bytes verwendet verfolgt.Ich möchte eine Benachrichtigung erhalten, wenn die total_bytes einen bestimmten Wert sagen 1000 Bytes erreicht haben. Ich habe über das Internet für eine Stunde gesucht und ich habe nichts nützliches gefunden. Wie gehe ich vor?wie ich eine Benachrichtigung auf meinem runnable

public class Data_usage extends AppCompatActivity { 
    private Handler mHandler = new Handler(); 

    private long mStartRX =  0; 

    private long mStartTX = 0; 

    long total_bytes; 
    Context context;  


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_data_usage); 

      if (mStartRX == TrafficStats.UNSUPPORTED || 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); 
      } 
     } 

    public final Runnable mRunnable=new Runnable() { 
     @Override 
     public void run() { 
      TextView RX = (TextView)findViewById(R.id.RX); 

      TextView TX = (TextView)findViewById(R.id.TX); 

      TextView Totalbytes=(TextView)findViewById(R.id.TOTALX); 

      final long rxBytes=TrafficStats.getMobileRxBytes()-mStartRX; 
      RX.setText(Long.toString(rxBytes)); 

      final long txBytes=TrafficStats.getMobileTxBytes()-mStartTX; 
      TX.setText(Long.toString(txBytes)); 

      Totalbytes.setText(Long.toString(total_bytes)); 

      long Txx=rxBytes; 
      long Rxx=txBytes; 

      total_bytes=Rxx+Txx; 

      mHandler.postDelayed(mRunnable, 1000); 
     } 
    }; 
} 
+0

Meinst du eine Benachrichtigung oder einen AlertDialog? Was geschieht bei der Ausführung? –

Antwort

0

Vielleicht haben Sie eine zweite Aktivität, die die Benachrichtigung ist.

public final Runnable mRunnable=new Runnable() { 

    @Override 
    public void run() { 

     TextView RX = (TextView)findViewById(R.id.RX); 

     TextView TX = (TextView)findViewById(R.id.TX); 

     TextView Totalbytes=(TextView)findViewById(R.id.TOTALX); 

     final long rxBytes=TrafficStats.getMobileRxBytes()-mStartRX; 
     RX.setText(Long.toString(rxBytes)); 

     final long txBytes=TrafficStats.getMobileTxBytes()-mStartTX; 
     TX.setText(Long.toString(txBytes)); 

     Totalbytes.setText(Long.toString(total_bytes)); 

     long Txx=rxBytes; 
     long Rxx=txBytes; 

     total_bytes=Rxx+Txx; 

     Intent myIntent = new Intent(this, Notification.Class); 
     Bundle bundle = myIntent.getExtras(); 
     bundle.putInt("bytes", total_bytes); 
     startActivity(myIntent); 

     mHandler.postDelayed(mRunnable, 1000); 


    } 
0

Änderungen in Runnable vornehmen.

public final Runnable mRunnable=new Runnable() { 
     @Override 
     public void run() { 
      TextView RX = (TextView)findViewById(R.id.RX); 

      TextView TX = (TextView)findViewById(R.id.TX); 

      TextView Totalbytes=(TextView)findViewById(R.id.TOTALX); 

      final long rxBytes=TrafficStats.getMobileRxBytes()-mStartRX; 
      RX.setText(Long.toString(rxBytes)); 

      final long txBytes=TrafficStats.getMobileTxBytes()-mStartTX; 
      TX.setText(Long.toString(txBytes)); 

      Totalbytes.setText(Long.toString(total_bytes)); 

      long Txx=rxBytes; 
      long Rxx=txBytes; 

      total_bytes=Rxx+Txx; 

      if(total_bytes==1000){ 
       // Display alert dialog here 

       mHandler.removeCallbacks(mRunnable); 
      }else{ 
       mHandler.postDelayed(mRunnable, 1000); 
      } 


     } 
    }; 

Hoffe, das wird Ihnen helfen.