Android获取当前运行的应用程序包名称

时间:2015-12-22 07:25:26

标签: android package android-context

我有以下代码在Android 5.0和6.0中正确运行但如果我将此应用程序用于Android 4.4设备,则应用程序将崩溃。请帮我解决这个问题。 以下是我的代码

 if (Build.VERSION.SDK_INT > 20) {
     packageName = mActivityManager.getRunningAppProcesses().get(1).processName;
 } else { 
     packageName = mActivityManager.getRunningTasks(1).get(0).topActivity.getPackageName();
 }
 Log.e("Package : ", "" + packageName);

错误显示如下,

  

E / AndroidRuntime:致命异常:主要                                                                       处理:com.keyboardsample,PID:23694                                                                       android.content.ActivityNotFoundException:找不到处理Intent的Activity {act = android.intent.action.SEND typ = image / png flg = 0x1 pkg = com.keyboardsample(has clip)(有附加内容)}                                                                           在android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1632)                                                                           在android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)                                                                           在android.app.Activity.startActivityForResult(Activity.java:3424)                                                                           在android.app.Activity.startActivityForResult(Activity.java:3385)                                                                           在android.app.Activity.startActivity(Activity.java:3627)                                                                           在android.app.Activity.startActivity(Activity.java:3595)                                                                           在com.keyboardsample.PhotoActivity $ 1.onItemClick(PhotoActivity.java:80)                                                                           在android.widget.AdapterView.performItemClick(AdapterView.java:299)                                                                           在android.widget.AbsListView.performItemClick(AbsListView.java:1113)                                                                           在android.widget.AbsListView $ PerformClick.run(AbsListView.java:2911)                                                                           在android.widget.AbsListView $ 3.run(AbsListView.java:3645)                                                                           在android.os.Handler.handleCallback(Handler.java:733)                                                                           在android.os.Handler.dispatchMessage(Handler.java:95)                                                                           在android.os.Looper.loop(Looper.java:136)                                                                           在android.app.ActivityThread.main(ActivityThread.java:5001)                                                                           at java.lang.reflect.Method.invokeNative(Native Method)                                                                           在java.lang.reflect.Method.invoke(Method.java:515)                                                                           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785)                                                                           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)                                                                           在dalvik.system.NativeStart.main(本地方法)

1 个答案:

答案 0 :(得分:4)

您可以使用

getApplicationContext().getPackageName();