public void action(){ Thread thread = new Thread(); @Override public void run(){ while(true){ try { Thread.sleep(10); }catch (InterruptedException e) { // TODO: handle exception e.printStackTrace(); } repaint(); } };
时间: 2024-04-09 21:27:45 浏览: 179
这段代码仍然存在一些问题。下面是对代码进行修正的建议:
```java
public void action() {
Thread thread = new Thread() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
repaint();
}
}
};
thread.start();
}
```
修正后的代码中,我们在创建线程对象时直接使用了匿名内部类来重写`run`方法。然后调用`start()`方法来启动线程。
请注意,这段代码依然需要在适当的上下文环境中使用,比如在一个继承自`Canvas`或`JPanel`的组件类中调用`action()`方法。
如果你有任何进一步的问题,请随时提问。
相关问题
在接收到ACTION_MEDIA_EJECT或者ACTION_MEDIA_UNMOUNTED广播后,可以通过以下步骤使U盘成功卸载,不会再下次插入的时候系统提示“vold : ForceUnmount failed: Device or resource busy”: 停止对U盘中工厂apk的读写操作,即关闭工厂apk应用程序。 关闭U盘中工厂apk应用程序所打开的所有文件和流。 关闭U盘中工厂apk应用程序所占用的所有资源。 等待一段时间,确保U盘中工厂apk应用程序已经完全释放了对U盘的占用。 卸载U盘。 另外,为了避免下次插入时提示“vold : ForceUnmount failed: Device or resource busy”,可以在卸载U盘后,检查U盘是否已经完全卸载,如果没有完全卸载,可以尝试强制卸载U盘。如果还是不行,可以重启设备再尝试卸载。 请详细写出代码实现方式
以下是示例代码实现方式:
```
public class MainActivity extends AppCompatActivity {
private static final String FACTORY_APK_NAME = "factory.apk";
private static final String FACTORY_APK_PACKAGE_NAME = "com.example.factory";
private static final int WAIT_TIME = 5000; // 等待时间,单位为毫秒
private boolean mIsFactoryApkRunning = false;
private boolean mIsUninstalling = false;
private BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (intent.getAction()) {
case Intent.ACTION_MEDIA_MOUNTED:
// 获取U盘中的工厂apk信息
String apkPath = getFactoryApkPath(context);
if (apkPath != null) {
// 启动工厂apk
startFactoryApk(context, apkPath);
}
break;
case Intent.ACTION_MEDIA_EJECT:
case Intent.ACTION_MEDIA_UNMOUNTED:
// 停止工厂apk
stopFactoryApk(context);
// 卸载U盘
uninstallUsb(context);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 注册U盘相关广播接收器
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_EJECT);
filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
filter.addDataScheme("file");
registerReceiver(mUsbReceiver, filter);
}
@Override
protected void onDestroy() {
super.onDestroy();
// 注销U盘相关广播接收器
unregisterReceiver(mUsbReceiver);
}
private String getFactoryApkPath(Context context) {
String apkPath = null;
File usbRoot = getUsbRoot(context);
if (usbRoot != null) {
File[] files = usbRoot.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.equals(FACTORY_APK_NAME);
}
});
if (files != null && files.length > 0) {
apkPath = files[0].getAbsolutePath();
}
}
return apkPath;
}
private File getUsbRoot(Context context) {
File usbRoot = null;
StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
try {
String[] volumePaths = (String[]) storageManager.getClass().getMethod("getVolumePaths").invoke(storageManager);
for (String path : volumePaths) {
File file = new File(path);
if (file.exists() && file.isDirectory() && file.canRead()) {
StatFs statFs = new StatFs(file.getAbsolutePath());
if (statFs.getBlockCountLong() > 0 && statFs.getBlockSizeLong() > 0 &&
statFs.getAvailableBlocksLong() * statFs.getBlockSizeLong() > 0) {
usbRoot = file;
break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return usbRoot;
}
private void startFactoryApk(Context context, String apkPath) {
if (mIsFactoryApkRunning) {
return;
}
Intent intent = context.getPackageManager().getLaunchIntentForPackage(FACTORY_APK_PACKAGE_NAME);
if (intent == null) {
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(apkPath)), "application/vnd.android.package-archive");
}
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
mIsFactoryApkRunning = true;
}
private void stopFactoryApk(Context context) {
if (!mIsFactoryApkRunning) {
return;
}
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> processInfoList = activityManager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : processInfoList) {
if (processInfo.processName.equals(FACTORY_APK_PACKAGE_NAME)) {
android.os.Process.killProcess(processInfo.pid);
break;
}
}
mIsFactoryApkRunning = false;
}
private void uninstallUsb(final Context context) {
if (mIsUninstalling) {
return;
}
mIsUninstalling = true;
new Thread(new Runnable() {
@Override
public void run() {
while (isFactoryApkRunning(context)) {
try {
Thread.sleep(WAIT_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
String path = getUsbRoot(context).getAbsolutePath();
String cmd = "pm uninstall " + path;
try {
Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
e.printStackTrace();
}
mIsUninstalling = false;
}
}).start();
}
private boolean isFactoryApkRunning(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> processInfoList = activityManager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : processInfoList) {
if (processInfo.processName.equals(FACTORY_APK_PACKAGE_NAME)) {
return true;
}
}
return false;
}
}
```
以上是一个简单的示例代码实现方式,具体实现方式可能会因为需求和环境的不同而有所不同。
阅读全文