2016-12-25 103 views
0

這是我的代碼,用於調用卸載程序Intent以獲取通過文本字段輸入的包名稱。使用onclick強制關閉從設備卸載應用程序?

的問題是,當我按下button我的應用程序強制關閉,下面是我的代碼 -

public void uninstall(View view) { 
    String packagename = seturl.getText().toString(); 
    Uri packageUri = Uri.parse(packagename); 
    Intent uninstallIntent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE, 
      packageUri); 
    startActivity(uninstallIntent); 
} 

日誌

12-24 22:18:43.456: E/AndroidRuntime(31831): FATAL EXCEPTION: main 
12-24 22:18:43.456: E/AndroidRuntime(31831): Process: com.example.xadb, PID: 31831 
12-24 22:18:43.456: E/AndroidRuntime(31831): java.lang.IllegalStateException: Could not execute method for android:onClick 
12-24 22:18:43.456: E/AndroidRuntime(31831): at android.view.View$DeclaredOnClickListener.onClick(View.java) 
12-24 22:18:43.456: E/AndroidRuntime(31831): at android.view.View.performClick(View.java) 
12-24 22:18:43.456: E/AndroidRuntime(31831): at android.widget.TextView.performClick(TextView.java) 
12-24 22:18:43.456: E/AndroidRuntime(31831): at android.view.View$PerformClick.run(View.java) 
12-24 22:18:43.456: E/AndroidRuntime(31831): at android.os.Handler.handleCallback(Handler.java) 
12-24 22:18:43.456: E/AndroidRuntime(31831): at android.os.Handler.dispatchMessage(Handler.java) 
12-24 22:18:43.456: E/AndroidRuntime(31831): at android.os.Looper.loop(Looper.java) 
12-24 22:18:43.456: E/AndroidRuntime(31831): at android.app.ActivityThread.main(ActivityThread.java) 
12-24 22:18:43.456: E/AndroidRuntime(31831): at java.lang.reflect.Method.invoke(Native Method) 
12-24 22:18:43.456: E/AndroidRuntime(31831): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java) 
12-24 22:18:43.456: E/AndroidRuntime(31831): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java) 
12-24 22:18:43.456: E/AndroidRuntime(31831): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102) 
12-24 22:18:43.456: E/AndroidRuntime(31831): Caused by: java.lang.reflect.InvocationTargetException 
12-24 22:18:43.456: E/AndroidRuntime(31831): at java.lang.reflect.Method.invoke(Native Method) 
12-24 22:18:43.456: E/AndroidRuntime(31831): ... 12 more 
12-24 22:18:43.456: E/AndroidRuntime(31831): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.UNINSTALL_PACKAGE dat=com.example.avd VirtualScreenParam=Params{mDisplayId=-1, null, mFlags=0x00000000)} } 
12-24 22:18:43.456: E/AndroidRuntime(31831): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java) 
12-24 22:18:43.456: E/AndroidRuntime(31831): at android.app.Instrumentation.execStartActivity(Instrumentation.java) 
12-24 22:18:43.456: E/AndroidRuntime(31831): at android.app.Activity.startActivityForResult(Activity.java) 
12-24 22:18:43.456: E/AndroidRuntime(31831): at android.app.Activity.startActivityForResult(Activity.java) 
12-24 22:18:43.456: E/AndroidRuntime(31831): at android.app.Activity.startActivity(Activity.java) 
12-24 22:18:43.456: E/AndroidRuntime(31831): at android.app.Activity.startActivity(Activity.java) 
12-24 22:18:43.456: E/AndroidRuntime(31831): at com.example.xadb.MainActivity.uninstall(MainActivity.java:61) 
12-24 22:18:43.456: E/AndroidRuntime(31831): ... 13 more 
+1

您的onclick方法中有'ActivityNotFoundException'。 Stacktrace告訴了這一切。 – Axxxon

+0

但我已經安裝了應用程序,我只是檢查 –

+0

@bad媽媽檢查我的答案,它應該可以幫助你 –

回答

0

問題是軟件包的名稱,它不是找到合適的人,先硬包裝(如果你的包名是com.android.yourpakgenamehere)名稱,看看它是否工作!

Uri packageURI = Uri.parse("package:com.android.yourpakgenamehere"); // see here how i used it 
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI); 
startActivity(uninstallIntent); 

然後你就可以得到它這樣,更換鐵桿名字,

全球對類:

public static String PACKAGE_NAME; 

然後..

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     PACKAGE_NAME = getApplicationContext().getPackageName(); 
    } 

You can then access it via `Main.PACKAGE_NAME`. // considering Activity name as Main 

現在你有包名,所以這樣做;

Uri packageUri = Uri.parse("package:"+ActivityYouUse.PACKAGE_NAME); 

無論如何,當你把它放在遊戲商店你能改變你的包名嗎?因此,即使該字符串值爲靜態

+0

它的工作原理,但是如何通過我的字符串設置包值?我的字符串不斷變化,所以我不想硬編碼它 –

+0

我只是想從我的編輯文本(seturl) –

+0

設置包名稱如果你發現它很難得到包名稱像這樣http://stackoverflow.com/ a/6589829/5188159然後'Uri packageUri = Uri.parse(「package:」+ ActivityYouUse.PACKAGE_NAME);' –