How to open google calendar app from my app?

2019-07-07 06:37发布

问题:

I want to open Google calendar app from my app. I have following code but it crashes:

if (Utils.isPackageInstalled(PACKAGENAME,getApplicationContext())) {
    intent = new Intent(Intent.ACTION_VIEW, Uri.parse( "com.android.calendar"));
    startActivity(intent);
}

LogCat:

04-30 09:14:29.156: E/AndroidRuntime(21225): FATAL EXCEPTION: main
04-30 09:14:29.156: E/AndroidRuntime(21225): Process: com.am, PID: 21225
04-30 09:14:29.156: E/AndroidRuntime(21225): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=com.android.calendar }
04-30 09:14:29.156: E/AndroidRuntime(21225):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1632)
04-30 09:14:29.156: E/AndroidRuntime(21225):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
04-30 09:14:29.156: E/AndroidRuntime(21225):    at android.app.Activity.startActivityForResult(Activity.java:3424)
04-30 09:14:29.156: E/AndroidRuntime(21225):    at android.app.Activity.startActivityForResult(Activity.java:3385)
04-30 09:14:29.156: E/AndroidRuntime(21225):    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:839)
04-30 09:14:29.156: E/AndroidRuntime(21225):    at android.app.Activity.startActivity(Activity.java:3627)
04-30 09:14:29.156: E/AndroidRuntime(21225):    at android.app.Activity.startActivity(Activity.java:3595)
04-30 09:14:29.156: E/AndroidRuntime(21225):    at com.amexbarcelo.activity.RootActiveTravel$4.onClick(RootActiveTravel.java:147)
04-30 09:14:29.156: E/AndroidRuntime(21225):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
04-30 09:14:29.156: E/AndroidRuntime(21225):    at android.os.Handler.dispatchMessage(Handler.java:102)
04-30 09:14:29.156: E/AndroidRuntime(21225):    at android.os.Looper.loop(Looper.java:136)
04-30 09:14:29.156: E/AndroidRuntime(21225):    at android.app.ActivityThread.main(ActivityThread.java:5017)
04-30 09:14:29.156: E/AndroidRuntime(21225):    at java.lang.reflect.Method.invokeNative(Native Method)
04-30 09:14:29.156: E/AndroidRuntime(21225):    at java.lang.reflect.Method.invoke(Method.java:515)
04-30 09:14:29.156: E/AndroidRuntime(21225):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-30 09:14:29.156: E/AndroidRuntime(21225):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-30 09:14:29.156: E/AndroidRuntime(21225):    at dalvik.system.NativeStart.main(Native Method)

回答1:

Its not advisable to use the package name for 3rd party application in intent for launching. You must specify the Action and URI and let the system find the best app to handle the request.

  1. Android Docs on using Calendar Intent,
  2. How to use Calendar Intent?,
  3. What You Need to Know About the Intents of Android’s Calendar App