Need App Indexing enabled sample code part

1.7k views Asked by At

I have working on App Indexing feature in my app and following the Google Developers guide. From the guide I have sticking on the following.

// Create App Indexing Link objects
AppIndexingLink item1 = new AppIndexingLink(appUri1, webUrl1, this.findViewById(R.id.btn1));
AppIndexingLink item2 = new AppIndexingLink(appUri2, webUrl2, this.findViewById(R.id.btn2)); 

From the above statements we can notice the parameters.

My Query on this:

  1. In that document they mentioned the web URL is optional. But when I remove that gives compilation error and if I make it empty ("") gives NullPointerException (Not sure the empty causes the NullPointerException )

2.The btn1 and btn2, where should we get those from? Why we have give them there? . If I give any view from my project yields NullPointerException(This is the situation which makes me not sure on the first one).

If any one have tutorial AND sample codes to enable the App Indexing , kindly provide it. Its may help us.

2

There are 2 answers

0
Rajnish Mishra On

1-webUrl1 is a object of Uri type you need to parse some url ex use your google play url

2-btn1 and btn2 can be any view the user is viewing example a image view or a text view containign some text

1
Mukesh On

Try this easier way of integrating appindex.

Add intentFilter to any of your activity that going to handle Appindex,

  <intent-filter >
                     <action android:name="android.intent.action.VIEW" />
                     <category android:name="android.intent.category.DEFAULT" />
                     <category android:name="android.intent.category.BROWSABLE" />

                     <data android:scheme="http"
                           android:host="www.yoursite.com"
                           android:pathPrefix="/"
                           />
      </intent-filter>

After that in that oncreate of that activity you can use

    Uri data = getIntent().getData();

This data will give the full url that landed to your app.

   System.out.println("Data URL: "+data);  

And also you can this uri data to get host name.

    if(data!=null)
    {
        String tmpData = data.toString();
        String hostName = data.getHost();
    }

Ensure that you have done site verification from webmaster tool before you make the app live.