Reputation: 3388
I am making a application which relies on a class which uses a BroadCastReceiver so that I can catch when a application is installed and uninstalled. The intent is received just fine, and the code in onReceive executes as it should. Hoever, the problem I am having is that I get a a error message when I try to instantiate a new PackageManger. The error is: "Cannot instantiate the type PackageManager". I've pretty much tried all variations, but I just can't get it to work.
my code:
package com.my.package;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.util.Log;
public class PackageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Uri datauri = intent.getData();
String uri = datauri.toString();
PackageManager pm = new PackageManager();
//pm.getApplicationInfo(uri); //broken
test(datauri); //works fine
}//end method
public void test(Uri uri){
Log.d("test", "data: "+uri);
}//end method
}// end class
Upvotes: 0
Views: 6176
Reputation: 132982
use
PackageManager pm= context.getPackageManager();
List<ApplicationInfo> applicationInfos = pm.getInstalledApplications(0);
....
instead of
PackageManager pm = new PackageManager();
Upvotes: 3