そのため、プラグインすると、ユーザーにデバイスをコンピューターから取り外すように指示するアクティビティを起動するアプリを作成しています。このクラスには、プラグインされているかどうかを定期的にチェックするタイマーがあります。タイマーのコードは、ユーザーがコードを外したときに別のアクティビティを開始しようとするとき以外は機能します。問題がタイマーのあるクラスにあるのか、マニフェストにあるのかはわかりませんが、切断時に起動しようとしているアクティビティは以前に機能していて、変更を加えていません。
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>