-3

in my project i use listview (list of program installed in device) and inside listview i have button for each row. when user clicked button i want to delete program. now in arrayadapter (custom list) i have (button.setOnClickListener) when user clicked button my unintall code must run pls help me

my uninstallApp class:

 public class UninstallApp extends ArrayAdapter<String>{
        public BlackListAdapter(Context context,int layoutResourceId, ArrayList<String> appsName,
        ArrayList<String> appsPackageName,ArrayList<Drawable> appsIcon) {
    super(context, layoutResourceId, appsName);

... . . . }

 @Override
public View getView(final int position, View convertView, final ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

                   btnDell.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {

        Uri packageUri =Uri.parse("package:com.mk88.rootdetection");

            Intent uninstallIntent =
                 new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageUri);
                  //  where runing startactivity ?????
            //startActivity(uninstallIntent);

        }
    });

    return rowView;
}


 } 
}

but when clicked button my program has stop. pls help me. tnx

2 Answers2

2

You should never instantiate Activity class

You have

  UninstallApp uninstallApp = new UninstallApp();

and UninstallApp is a Activity class.

Can i Create the object of a activity in other class?

Activity are declared in manifest and it has a lifecyle.

You can make UninstallApp a utility class (normal java class). If you need context you can pass the same to constructor of the class and use the context. startActivity is a method of activity class.

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • if my uninstallapp code run in mainActivity that is Correct runs but i need another activity to uninstall the app – user3555545 Jun 24 '14 at 08:32
  • @user3555545 then you need to start that activity not by instantiating the same. You never override any of the methods of activity like `onCreate`.. – Raghunandan Jun 24 '14 at 08:34
  • @user3555545 unless you want another screen you don't require Activity class – Raghunandan Jun 24 '14 at 08:35
  • @user3555545 what has that got to do with instantiating activity class?? – Raghunandan Jun 24 '14 at 08:40
  • in my project i use listview of program installed in device and inside listview i have button for each row. when user clicked button i want to delete program. now in arrayadapter (custom list) i have button.setOnClickListener when user clicked button my unintall code must run pls help me – user3555545 Jun 24 '14 at 08:45
  • then why do you need a activity class. Make it a normal java class and pass info needed to the construtor of that. – Raghunandan Jun 24 '14 at 08:51
  • how?! startactivity need activity class – user3555545 Jun 24 '14 at 08:54
  • @user3555545 can't you pass the context to the constructor of the uninstallapp class. Then use the same to startActivity – Raghunandan Jun 24 '14 at 08:55
  • @user3555545 glad the post helped – Raghunandan Jun 24 '14 at 10:01
  • i use this post in new project with 2 class. MainActivity And Uninsallapp. in mainactivity,call uninstall function, in uninstall function , call MainActivity.getInstance().dell() and this is ok and run. – user3555545 Jun 24 '14 at 10:21
  • @user3555545 a crash. post the stacktrace or ask a different question – Raghunandan Jun 24 '14 at 10:51
0

I don't think you need of another activity to uninstall the app.Also you should not instantiate Activity. Use the below code in your main activity or Utility Class

 Uri packageURI = Uri.parse("package:" +getPackageName());
 Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);      
 startActivity(uninstallIntent);

Hope it helps.

vinothp
  • 9,939
  • 19
  • 61
  • 103