sync edittext与intentservice包

ftf50wuq  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(206)

我需要创建一个计时器,当计时器变成0的应用程序应该关闭。计时器工作正常,但每当活动重新启动时,负责显示用户当前倒计时时间的editext就会设置为空。我放了一杯酒来检查负责上述品尝的功能是否正常,是否正常。
我不明白更新editext有什么问题。
时效性

public class TimerActivity extends AppCompatActivity {
public static final int RESULT_CODE = 11;
Handler h = new Handler();
public EditText t , t2;
Button start;
String time;
int time_sec = 0;
private Handler mHandler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_timer);
    t = findViewById(R.id.editText);
    t2 = findViewById(R.id.editText2);
    start = findViewById(R.id.timer_btn);

    start.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            time = t2.getText().toString().trim();
            if (Integer.parseInt(time) <= 180 && Integer.parseInt(time) > 0 )
            {
                time_sec = Integer.parseInt(time) * 60;

                startMyService(v);
            }
            else{

                Toast.makeText(getBaseContext() , "Maximum time limit is 180 mins." ,Toast.LENGTH_SHORT).show();
            }
        }
    });
}

public void startMyService(View v)
{
    Intent ii = new Intent(this , MyIntentService.class);
    ResultReceiver r = new myreciever(null);
    ii.putExtra("counter" , 2);
    ii.putExtra("receiver" , r);
    ii.putExtra("time" , String.valueOf(time_sec) );
    startService(ii);
}

public void stopMyService(View v)
{

}

public class myreciever extends ResultReceiver
{

    /**
     * Create a new ResultReceive to receive results.  Your
     * {@link #onReceiveResult} method will be called from the thread running
     * <var>handler</var> if given, or from an arbitrary thread if null.
     *
     * @param handler
     */
    public myreciever(Handler handler) {
        super(handler);
    }

    @Override
    protected void onReceiveResult(int resultCode, Bundle resultData) {
        if (resultCode == RESULT_CODE)
        {
            if (resultData != null )
            {
                final String msg = resultData.getString("result");
                h.post(new Runnable() {

                    public void run() {
                     if (Integer.parseInt(msg) > 0)
                        {
                            int seconds = Integer.parseInt(msg);
                            int numberOfMinutes = ((seconds % 86400 ) % 3600 ) / 60;
                            int numberOfSeconds = ((seconds % 86400 ) % 3600 ) % 60;
                            t.setText(String.valueOf(numberOfMinutes) + " : " + String.valueOf(numberOfSeconds););
                            Toast.makeText(getBaseContext() , "Running" ,Toast.LENGTH_SHORT).show();
                        }
                        else
                        {
                            Toast.makeText(getBaseContext() , "Stop" ,Toast.LENGTH_SHORT).show();
                            stopService(new Intent( getBaseContext() , OnClearFromRecentService.class));
                            moveTaskToBack(true);
                            android.os.Process.killProcess(android.os.Process.myPid());
                            System.exit(1);

                        }
                    }
                });
            }
        }
    }
}

}
myintentservice公司

public class MyIntentService extends IntentService {

private static final String TAG = "MyIntentService";
int cstart = 0;
int time = 0;
public MyIntentService() {
    super("Service Demo");
}

protected void onHandleIntent( Intent intent) {

    ResultReceiver rr = intent.getParcelableExtra("receiver");
    time = Integer.parseInt(intent.getStringExtra("time"));

    Bundle b = new Bundle();
    b.putString("result" , String.valueOf(time) );
    rr.send(TimerActivity.RESULT_CODE , b );

   // TimerActivity obj = new TimerActivity();

    while (time >= 0 )
    {

      //  obj.t2.setText(String.valueOf(time));

        Log.v(TAG , "counter now is : " + time);
        try{
            Thread.sleep(1000);
        }catch (InterruptedException e)
        {
            e.printStackTrace();
        }

      b.putString("result" ,  String.valueOf(time) );
        rr.send(TimerActivity.RESULT_CODE , b );

        time-- ;
    }

}

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题