Search code examples
javagoogle-chromejava-native-interfacebuild.gradle

Java - Launch Chrome on any Android Phone


I'm just getting started with java. I'm looking to open chrome to a specific page using a JNI from a button in the UI. I'm getting an error that the chrome file cannot be found. My questions are:

  1. Can be chrome be found in the same place on all android phones?
  2. If so, what is the path?
  3. If not, how would you approach finding the path to chrome here?
  4. Is there an import required on this page to make this function happen?
  5. Do I need to enable a permission in my app package to achieve this?
Runtime runtime = Runtime.getRuntime();    
     String[] s = new String[] {"\\system\\app\\Chrome\\Chrome.apk", "https://www.google.com"}; 
           try
           {
            runtime.exec(s);        
           }
            catch (IOException e)
           {
            e.printStackTrace();
           }

and then the error

W System.err: java.io.IOException: Cannot run program "\system\app\Chrome\Chrome.apk": error=2, No such file or directory

Thank you in advance!


Solution

  • You are not using JNI, you are trying to execute an android as it were an executable using the Runtime.exec() method. I don't think that will work in any case. You should try using Android functionality to open Chrome, not methods that would work on other operating systems like Windows or Linux. See this answer for some supported methods: https://stackoverflow.com/a/12013755/721855