android - How to start Google API - ActivityRecognitionAPI using BroadcastRecievers on device boot? -
i have used api using activities , works. tried starting mainactivity using broadcastreciever , works. when try this, crashes app.
do need add flags intent? how in case?
i tried these codes app crashes on boot:
1) broadcastreciever class:
public class startreciever extends broadcastreceiver implements googleapiclient.connectioncallbacks, googleapiclient.onconnectionfailedlistener { public context appcontext; public googleapiclient mapiclient; @override public void onreceive(context context, intent intent) { appcontext = context; mapiclient = new googleapiclient.builder(context) .addapi(activityrecognition.api) .addconnectioncallbacks((googleapiclient.connectioncallbacks) context) .addonconnectionfailedlistener((googleapiclient.onconnectionfailedlistener) context) .build(); mapiclient.connect(); } @override public void onconnected(@nullable bundle bundle) { intent intent = new intent(appcontext, activityrecognizedservice.class); pendingintent pendingintent = pendingintent.getservice(appcontext, 0, intent, pendingintent.flag_update_current); activityrecognition.activityrecognitionapi.requestactivityupdates(mapiclient, 10000, pendingintent); } @override public void onconnectionsuspended(int i) { toast.maketext(appcontext, "connection google services suspended!", toast.length_long).show(); mapiclient.reconnect(); } @override public void onconnectionfailed(@nonnull connectionresult connectionresult) { toast.maketext(appcontext, "connection google services failed!", toast.length_long).show(); mapiclient.connect(); }
2) activityrecognizedservice
public class activityrecognizedservice extends intentservice { public activityrecognizedservice() { super("activityrecognizedservice"); } public activityrecognizedservice(string name) { super(name); } @override protected void onhandleintent(intent intent) { if (activityrecognitionresult.hasresult(intent)) { activityrecognitionresult result = activityrecognitionresult.extractresult(intent); handledetectedactivities(result.getprobableactivities()); } } private void handledetectedactivities(list<detectedactivity> probableactivities) { (detectedactivity activity : probableactivities) { switch (activity.gettype()) { case detectedactivity.in_vehicle: { log.e("activityrecogition", "in vehicle: " + activity.getconfidence()); break; } case detectedactivity.on_bicycle: { log.e("activityrecogition", "on bicycle: " + activity.getconfidence()); break; } case detectedactivity.on_foot: { log.e("activityrecogition", "on foot: " + activity.getconfidence()); break; } case detectedactivity.running: { log.e("activityrecogition", "running: " + activity.getconfidence()); break; } case detectedactivity.still: { log.e("activityrecogition", "still: " + activity.getconfidence()); notificationcompat.builder builder = new notificationcompat.builder(this); builder.setcontenttext("are walking?"); builder.setsmallicon(r.mipmap.ic_launcher); builder.setcontenttitle(getstring(r.string.app_name)); notificationmanagercompat.from(this).notify(0, builder.build()); break; } case detectedactivity.tilting: { log.e("activityrecogition", "tilting: " + activity.getconfidence()); break; } case detectedactivity.walking: { log.e("activityrecogition", "walking: " + activity.getconfidence()); break; } case detectedactivity.unknown: { log.e("activityrecogition", "unknown: " + activity.getconfidence()); break; } } } }
for start, used api used in link: http://code.tutsplus.com/tutorials/how-to-recognize-user-activity-with-activity-recognition--cms-25851
add necessary permissions in adnroidmanifest.xml:
<uses-permission android:name = "android.permission.receive_boot_completed"/> <uses-permission android:name="android.permission.wake_lock"/>
then need link boot message particular broadcastreceiver, receive , process message("boot") issued phone. can define broadcast receiver extension of wakefulbroadcastreceiver, ensures device stay stay awake until service started e.g.
public class mybroadcastreceiver extends wakefulbroadcastreceiver { @override public void onreceive(context con, intent i) { intent intent = new intent(con, myintentservice.class); startwakefulservice(con, intent); } }
after that, declare receiver in manifest file:
<receiver android:name="com.example.mybroadcastreceiver"> <intent-filter> <action android:name="android.intent.action.boot_completed" /> </intent-filter> </receiver>
boot_completed message ensures our receiver launched on device boot up. when boot message received, "wakeful" receiver launches service. need release wake lock inside onhandleintent device can go sleep after service launched:
public class myintentservice extends intentservice { @override protected void onhandleintent(intent i) { //release wake lock wakefulbroadcastreceiver.completewakefulintent(i); } }
now service start automatically, whenever device boots
Comments
Post a Comment