Redirect chrome programmatically

2020-07-26 11:18发布

问题:

I have been able to read the chrome content using accessibility service using the below code

<accessibility-service   xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityFlags="flagDefault"
android:accessibilityEventTypes="typeWindowStateChanged"
android:accessibilityFeedbackType="feedbackGeneric"
android:notificationTimeout="0"
android:canRetrieveWindowContent="true"
android:packageNames="com.android.chrome"
android:description="@string/accessibility_description"
/>

And I listen to window changed event using

public void onAccessibilityEvent(AccessibilityEvent event) {
if(AccessibilityEvent.eventTypeToString(event.getEventType()).contains("WINDOW")){
     AccessibilityNodeInfo nodeInfo = event.getSource();
     dfs(nodeInfo);
}
}

public void dfs(AccessibilityNodeInfo info){
if(info == null)
    return;
if(info.getText() != null && info.getText().length() > 0)
    System.out.println(info.getText() + " class: "+info.getClassName());
for(int i=0;i<info.getChildCount();i++){
   AccessibilityNodeInfo child = info.getChild(i);
   dfs(child);
   child.recycle();
}
}

Now based on URL content , I want to open a redirect chrome to a different page or load a static page after fetching it from the device storage. I tried to achieve it but have been unsuccessful till now. Any help would be appreciated. Thanks in advance.

回答1:

Try this code

 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(Constant.URL_REPLACE));
        intent.setPackage("com.android.chrome");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(Browser.EXTRA_APPLICATION_ID,"com.android.chrome");
        try {
            mContext.startActivity(intent);
        } catch (ActivityNotFoundException ex) {
            Log.e(TAG, "Exception = " + ex.toString());
        }