0

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

下面是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>

4

1 回答 1

1

使用runOnUiThread从 TimerTask 线程访问 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);
于 2012-12-27T05:26:45.973 回答