I am trying to add an open source game to my application. I mean, I developed an application. When a user installs my application, there will be button in the app and when开发者_如何学Go user taps the button, internal apk (open source game) will be started.
But, I dont know where I should put the game apk file to? I dont know how I can invoke the game apk?
I found following code which starts any apk from sdcard (I guess).
String fileName = Environment.getExternalStorageDirectory() + "/myApp.apk";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");
startActivity(intent);
But, I plan to prepare one APK (and game will be put somewhere such res/raw).
there will be button in the app and when user taps the button, internal apk (open source game) will be started.
If you mean, that the application (the internal apk) will be started, then you don't understand how that works. So let me explain: an APK is not like an .EXE on a Windows system. I mean, you cannot just "run" an APK.
An APK is a package which contains an application, and the only thing you can do with it is install it. The code you post above seems to invoke the android application installer, which would do that: install the app (not run it). As you can see, it's necessary that the app is in the external storage (SD card), and that's because the app installer cannot access your /res/raw directly.
So, what you have to do is copy the APK from res/raw to the SDCard (Google is your friend), and then use the code above. Again, that won't execute the app, it will just install it. Once it's installed, you can execute it from your app doing something lile:
Intent intent = new Intent();
intent.setClassName("com.android.bla", "com.android.bla.YourActivity");
startActivity(intent);
精彩评论