我希望这是这个问题的恰当标题。现在我有两节课。一个主类和一个applicationoperations类。最初我有一个方法uninstallapplication和listall安装的应用程序,在我的主类中运行良好。现在,当我把它们移到另一个类(为了扩展)时,它们不起作用。我不知道我的意图,因为我仍然是一个新手,所以任何帮助都将是伟大的!
我有个错误
PackageManager pm = getPackageManager(); <--method undefined???
and
StartActivity <---method undefined.
这是密码。如果您还需要什么,请告诉我:
主文件:
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");
}
}
}
最佳答案
当你的代码
PackageManager pm = getPackageManager();
在从活动类(类main)继承的类中,它起作用,因为实际上getpackagemanager()是context.getpackagemanager。activity扩展了context类,因此一切正常。
但您将此代码移到了新的类中,该类不扩展任何与上下文相关的内容。因此有一个编译错误。
您应该以某种方式将上下文传递到新类(applicationoperations)中并使用它
PackageManager pm = mContext.getPackageManager()