3

I have below code which run correctly in Android 5.0 and 6.0 but if i am using this application to Android 4.4 device then application goes to crash. Please help me to solve the issue. Below is my code

 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);

Error showing like this,

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.keyboardsample, PID: 23694 android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SEND typ=image/png flg=0x1 pkg=com.keyboardsample (has clip) (has extras) } at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1632) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424) at android.app.Activity.startActivityForResult(Activity.java:3424) at android.app.Activity.startActivityForResult(Activity.java:3385) at android.app.Activity.startActivity(Activity.java:3627) at android.app.Activity.startActivity(Activity.java:3595) at com.keyboardsample.PhotoActivity$1.onItemClick(PhotoActivity.java:80) at android.widget.AdapterView.performItemClick(AdapterView.java:299) at android.widget.AbsListView.performItemClick(AbsListView.java:1113) at android.widget.AbsListView$PerformClick.run(AbsListView.java:2911) at android.widget.AbsListView$3.run(AbsListView.java:3645) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5001) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method)

starkshang
  • 8,228
  • 6
  • 41
  • 52
Mohd Sakib Syed
  • 1,679
  • 1
  • 25
  • 42

1 Answers1

7

You can use

getApplicationContext().getPackageName();