Reputation: 2151
I hope this is the appropriate title for this question. Right now I have two classes. A main class and a ApplicationOperations class. Initially I had a method uninstallapplication and listall installed applications which worked fine in my main class. Now as I move them to another class (in order to expand) they do not work. I do not know anything about intents as I am still a newbie so any help would be great!
I am getting an error on
PackageManager pm = getPackageManager(); <--method undefined???
and
StartActivity <---method undefined.
Here is the Code. Please let me know if you need anything else:
MAIN FILE:
package com.IPR2.viewlog;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Main extends Activity {
private TextView tv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
FileOperations fileOperations = new FileOperations();
ApplicationOperations applicationOperations = new ApplicationOperations();
Testing testing = new Testing();
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.TextView01);
}
}
ApplicationOperations.java
package com.IPR2.viewlog;
import java.util.List;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.util.Log;
public class ApplicationOperations {
FileOperations fileOperations = new FileOperations();
public void UninstallApplication(String packageName)
{
Uri packageURI = Uri.parse("package:"+packageName);
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
startActivity(uninstallIntent);
}
public void ListAllInstalledApplications()
{
PackageManager pm = getPackageManager();
//get a list of installed apps.
List<ApplicationInfo> packages = pm
.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages)
{
fileOperations.AppendFile("Installed package :" +
packageInfo.packageName + "\n", "Installed_packages.txt");
// tv.append("Installed package :" + packageInfo.packageName + "\n");
}
}
}
Upvotes: 2
Views: 1061
Reputation: 3414
getPackageManager() need Context.
You can add a Context or Main member in ApplicationOperations class and add a init method to pass Main to it.
private Main context;
public ApplicationOperations(Main activity) {
this.context = activity;
}
Then use it when needed
PackageManager pm = context.getPackageManager();
Upvotes: 2
Reputation: 1896
When your code
PackageManager pm = getPackageManager();
Was inside the Class inherited from Activity class (class Main), it worked because in fact getPackageManager() is Context.getPackageManager. Activity extends Context class, hence everything is okay. But you moved this code into new class which doesn't extend anything related to Context. Hence you have a compile error. You should pass somehow a context into your new class (ApplicationOperations) and use it
PackageManager pm = mContext.getPackageManager()
Upvotes: 3