|
MyTracksApi
MyTracks API allows third-party Android applications to access MyTracks data and start/stop a recording.
Featured, Phase-Implementation IntroductionMyTracks API consists of three mechanisms, content provider, service, and notifications, to allow third-party apps to access and control MyTracks data. Access to the MyTracks API is protected through the Android permissions. In addition to the permissions, MyTracks has a setting inside the MyTracks app to allow users to enable and revoke access to third-party apps. To modify the setting, from the MyTracks menu, go to Settings -> Sharing -> Allow access. Note that Android will only grant permissions to a third-party apps if MyTracks is already installed when installing the third-party app. Through the content provider, third-party apps can read/write the MyTracks database. Through the service, these apps can start/stop the MyTracks recording service. Finally, through the notifications, these apps can get notified when a recording has started/stopped. SetupMyTracks provides the MyTracksLib to make it easier for third-party apps to access MyTracks. The following assumes you are using Eclipse for your Android development.
hg clone https://mytracks.googlecode.com/hg mytracks MyTracks Content Provider
<uses-permission android:name="com.google.android.apps.mytracks.READ_TRACK_DATA" /> <uses-permission android:name="com.google.android.apps.mytracks.WRITE_TRACK_DATA" /> You can access the content provider by getting a ContentResolver, through getContentResolver, and calling the various methods like query and insert and passing in the appropriate URIs. However, MyTracks provides an utility library, MyTracksProviderUtils, to make it easier to invoke the content provider. private MyTracksProviderUtils myTracksProviderUtils;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myTracksProviderUtils = MyTracksProviderUtils.Factory.get(this);
}
@Override
protected void onStart() {
super.onStart();
List<Track> tracks = myTracksProviderUtils.getAllTracks();
for (Track track : tracks) {
System.out.println("Track id " + track.getId());
}
}MyTracks Service
<uses-permission android:name="com.google.android.apps.mytracks.WRITE_TRACK_DATA" /> To use the service, you need to first start the service, then bind it. Afterwards, you can invoke the various methods of the service. To cleanup, you need to unbind and stop the service. private ITrackRecordingService myTracksService;
private Intent intent;
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder service) {
myTracksService = ITrackRecordingService.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName className) {
myTracksService = null;
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
intent = new Intent();
intent.setComponent(new ComponentName(
getString(R.string.mytracks_service_package),
getString(R.string.mytracks_service_class)));
}
@Override
protected void onStart() {
super.onStart();
startService(intent);
bindService(intent, serviceConnection, 0);
}
@Override
protected void onStop() {
super.onStop();
if (myTracksService != null) {
unbindService(serviceConnection);
}
stopService(intent);
}
/*
* Note that bindService is asynchronous. After it completes,
* then can invoke the service's methods.
*/
myTracksService.startNewTrack();
myTracksService.endCurrentTrack();MyTracks Notifications
<uses-permission android:name="com.google.android.apps.mytracks.TRACK_NOTIFICATIONS" /> public class MyTracksReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
long trackId = intent.getLongExtra(context.getString(R.string.track_id_broadcast_extra), -1L);
Toast.makeText(context, action + " " + trackId, Toast.LENGTH_LONG).show();
}
}Sample AppSee MyTracksApiSample on the top level directory of the MyTracks code base. |
Currently, MyTracks? has the "Allow access" option in the sharing settings that governs whether or not third-party applications can start/stop MyTracks? recording. It's good that the setting is there since it protects MyTracks? from being misused by third-party applications. But at the same time, it causes a lot of problems for third-party applications that wish to make use of MyTracks?. The main problem is that users just install the third-party apps, and forget to set that option. Is there a way for the third-party apps to query whether or not the "allow access" option has been set? If there was a way, we could query then remind the user to go to the MyTracks? settings and allow access. Without that our applications just seem to fail from the user's point of view, just to find out later that it was because they needed to set that option.
It seems that to get the MyTracks? Service sample to work, I have to request the READ_TRACK_DATA permission as well. Any reason I should expect otherwise?