简体   繁体   English

如何设置通知以在我的可运行设备上弹出

[英]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. 我正在创建一个跟踪使用的字节数的应用程序。我想在total_bytes达到某个值(例如1000字节)时收到通知。 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. 在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. 希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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