2017-05-16 49 views
0

我有一個應用程序需要數據清理再次運行得更好。 我使用這個數據清除,清除緩存和數據後的Android重新啓動應用程序

((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(); 
+0

從那裏你叫清除緩存代碼這個代碼? –

+0

從上面的同一活動。也就是說,MainActivity – Vivek

+0

您的清除緩存代碼是否完美工作? –

回答

0

對於清除高速緩存嘗試引用link

嘗試下面的代碼重新啓動應用程序

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

注:如果使用清除應用程序數據應用不重新啓動。您必須手動重新啓動它。

1

調用clearApplication數據後,應用程序被終止。這就是爲什麼MainActivity沒有調用。

2

像下面

public class ApplicationClass extends Application { 

private static ApplicationClass instance; 

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

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

添加應用程序類清單

的Android的應用程序標籤創建新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> 
「ApplicationClass。」

並用於清除數據

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(); 
相關問題