计时器任务中的意图导致强制关闭

时间:2012-12-27 05:07:18

标签: android android-intent timer manifest

所以我正在编写一个应用程序,当插入时,它应该启动一个活动,告诉用户从计算机上拔下设备。在这个类中,我有一个定时器,它定期检查它是否插入。定时器中的代码工作,而不是当我尝试在用户断开电源线时启动另一个活动。我不确定这个问题是否存在于具有计时器或Manifest的类中,尽管我在断开连接时尝试启动的活动更早,而且我没有对它进行任何更改。

以下是 StateCheck 类的代码(通过检查SD卡的状态来测试它是否插入):

public class StateCheck extends Activity {

TextView stateCheck, unmountedImage, tester;
Timer timer;
int i = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.state_check);
    stateCheck = (TextView) findViewById(R.id.tvStateCheck);
    unmountedImage = (TextView) findViewById(R.id.tvUnmountedImage);
    tester = (TextView) findViewById(R.id.tvTester);

    //Intent openStartingPoint = new Intent("com.flannigan.MUSERCISEACTIVITY");
    //startActivity(openStartingPoint);

    MyTimerTask myTask = new MyTimerTask();
    timer = new Timer();
    timer.schedule(myTask, 1000);
}

class MyTimerTask extends TimerTask {
      public void run() {
          String state = Environment.getExternalStorageState();
          i++;

            if (Environment.MEDIA_MOUNTED.equals(state)) {
                unmountedImage.getHandler().post(new Runnable() {
                    public void run() {
                        unmountedImage.setVisibility(View.INVISIBLE);
                    }
                });
                stateCheck.getHandler().post(new Runnable() {
                    public void run() {
                        stateCheck.setText("SD card is mounted!\nLoading...");
                    }
                });
                Intent openStartingPoint = new Intent("com.flannigan.MUSERCISEACTIVITY");
                startActivity(openStartingPoint);           
            } else {
                MyTimerTask myTask = new MyTimerTask();
                timer = new Timer();
                timer.schedule(myTask, 1000);
                tester.getHandler().post(new Runnable() {
                    public void run() {
                        tester.setText(""+i);
                    }
                });
            }
      }
}
}

清单:          

<uses-sdk android:minSdkVersion="8" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".Splash"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".StateCheck"
        android:label="Musercise"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="com.flannigan.STATECHECK" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    <activity
        android:name=".MuserciseActivity"
        android:label="Musercise"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="com.flannigan.MUSERCISEACTIVITY" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

</application>

1 个答案:

答案 0 :(得分:1)

使用runOnUiThread从TimerTask Thread访问UI元素:

StateCheck.this.runOnUiThread(new Runnable() {
            public void run(){

             // start next Activity here
             // Access UI element here

            }          
        });    

并且用于启动活动将您的意图更改为:

Intent intent=new (StateCheck.this,MuserciseActivity.class);
startActivity(intent);