与IntentService捆绑包同步Editext

问题描述

我需要创建一个计时器,当计时器变为0时,应用应关闭。计时器可以正常工作,但是只要活动重新启动,负责显示用户当前倒计时时间的editext就会设置为空。 我举杯祝酒,以检查上述口味的功能是否正常运行,并且工作正常。

我不知道更新editext会有什么问题。

TimerActivity

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


}

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)