Android清除缓存和数据后如何重启应用程序

3

我有一个需要数据清洗才能更好运行的应用程序。 我正在使用以下清理数据,

((ActivityManager)MainActivity.this.getSystemService(ACTIVITY_SERVICE)).clearApplicationUserData();

但无法重新启动应用程序。我尝试在清除数据后添加意图。但是,由于在清除数据后应用程序关闭,因此我认为该代码是不可达的。

((ActivityManager)MainActivity.this.getSystemService(ACTIVITY_SERVICE)).clearApplicationUserData();
Toast.makeText(MainActivity.this,"Reloading...",Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this,MainActivity.class));
finish();

你从哪里调用了清除缓存的代码? - Bhavin Patel
从上面的同一个活动中。也就是说,MainActivity。 - Vivek
你的清除缓存代码是否完美运行? - Bhavin Patel
@Vivek,它解决了你的问题吗? - jagapathi
3个回答

1
调用clearApplicationData后,应用程序被终止。这就是为什么MainActivity没有被调用的原因。

1
创建新的ApplicationClass,如下所示:
public class ApplicationClass extends Application {

private static ApplicationClass instance;

@Override
public void onCreate() {
    super.onCreate();
    instance = this;
}

public static ApplicationClass getInstance() {
    return instance;
}
}

将Application类添加到清单文件中的Application标签

android:name=".ApplicationClass"

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:name=".ApplicationClass"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

使用此代码清除数据。
Intent intent = new Intent(MainActivity.this, MainActivity.class);
         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                 | Intent.FLAG_ACTIVITY_CLEAR_TASK
                 | Intent.FLAG_ACTIVITY_NEW_TASK);
         PendingIntent pendingIntent = PendingIntent.getActivity(ApplicationClass.getInstance().getBaseContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);
         AlarmManager mgr = (AlarmManager) ApplicationClass.getInstance().getBaseContext().getSystemService(Context.ALARM_SERVICE);
         mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, pendingIntent);
         System.exit(2);
         ((ActivityManager)MainActivity.this.getSystemService(ACTIVITY_SERVICE)).clearApplicationUserData();

尝试过这个,我认为调用 System.exit(2) 阻止了 clearApplicationUserData() 的执行。 - Robert
.ClearApplicationUserData(); 强制关闭应用程序。所以在这行代码之前我重新启动了应用程序。但是,代码运行得非常完美。 - jagapathi
3
这个答案是错误的。这段代码不起作用,而且永远也不会起作用。clearApplicationUserData会清除所有闹钟。 - Dmytro Karataiev
@Brian 我不记得来源了,但当时我通过手动测试确认了这一点。无论如何,这是一个可怕的解决方案,不应在生产中使用。 - Dmytro Karataiev
@DmytroKarataiev 谢谢您的更新。然而,我需要使用它来满足软件要求,从设备中删除所有我们的应用程序数据。 - Brian
显示剩余4条评论

0

清除缓存请参考链接

尝试以下代码重新启动应用程序

Intent i = getBaseContext().getPackageManager()
         .getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();

注意:如果您使用“清除应用数据”功能,应用程序不会自动重新启动。您需要手动再次启动它。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接