简体   繁体   中英

how to set up a notification to pop up on my runnable

i am creating an application that keeps track of the number of bytes used.i want to receive a notification when the total_bytes have reached a certain value say 1000 bytes. i have searched over the internet for about an hour and i havent found anything usefull. how do i go about it?

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

Maybe have a second Activity that is the notification.

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


    }

Make changes in 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);
           }


        }
    };

Hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM