2016-07-23 21 views
1

我正在創建一個跟蹤所用字節數的應用程序。我希望在total_bytes達到某個值(例如1000字節)時收到通知。我通過互聯網搜索了大約一個小時,而我還沒有找到任何有用的東西。我該如何去做呢?如何設置通知在我的可運行時彈出

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

你是指通知還是AlertDialog?另外,執行時目前發生了什麼? –

回答

0

也許有第二個Activity是通知。

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

對Runnable進行更改。

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); 
      } 


     } 
    }; 

希望這會幫助你。

相關問題