android - Starting a BroadcastReceiver from activity -
i'm trying start broadcastreceiver activity - , seems receiver doesnt start , dont output in logcat it. im trying start receiver doing this:
intent intent = new intent(); intent.setaction(getpackagename()+".mybroadcast"); sendbroadcast(intent);
also here receiver class:
public class triggerreceiver extends broadcastreceiver implements googleapiclient.connectioncallbacks, googleapiclient.onconnectionfailedlistener, locationlistener { private vibrator v; private countdowntimer timer; private final long interval = 1000; private final long two_minutes = interval * 60 * 2; // === variables private context context; private final string tag = triggerreceiver.class.getsimplename(); @override public void onreceive(context context, intent intent) { this.context = context; log.e(tag, "trigger receiver starting"); v = (vibrator) context.getsystemservice(context.vibrator_service); mrequestinglocationupdates = false; buildgoogleapiclient(); mgoogleapiclient.connect(); } // private void starttimer(final location location) { string message = context.getstring(r.string.emergency_text).replace("/location here/", getmaplink(location)); sendsms(message); shownotification(); log.e(tag, " sending sms , location " + location.getlatitude() + " " + location.getlongitude()); timer = new countdowntimer(two_minutes, interval) { @override public void ontick(long millisuntilfinished) { log.e(tag, "tick " + (millisuntilfinished / 1000)); int elapsedtime = (int) ((millisuntilfinished / 1000) % 60); int minutes = (int) ((millisuntilfinished / 1000) / 60); log.e(tag, elapsedtime + " | " + minutes); if (minutes == 2) { minutes = 1; elapsedtime = 60; } } //todo start activity , pass extras intent @override public void onfinish() { starttimer(mcurrentlocation); } }.start(); } private void sendsms(string message) { arraylist<contact> contacts = ((baseactivity) context).getcontacts(); (contact contact : contacts) { sendsmsmsg(contact.getnumber(), message); } } private void stoptimer() { timer.cancel(); } /*location*/ /** * provides entry point google play services. */ protected googleapiclient mgoogleapiclient; /** * stores parameters requests fusedlocationproviderapi. */ protected locationrequest mlocationrequest, quicklocationrequest; /** * represents geographical location. */ protected location mcurrentlocation, mlastlocation; protected boolean mrequestinglocationupdates; /** * builds googleapiclient. uses {@code #addapi} method request * locationservices api. */ protected synchronized void buildgoogleapiclient() { log.i(tag, "building googleapiclient"); mgoogleapiclient = new googleapiclient.builder(context) .addconnectioncallbacks(this) .addonconnectionfailedlistener(this) .addapi(locationservices.api) .build(); createlocationrequest(); } protected void createlocationrequest() { locationrequest mlocationrequest = new locationrequest(); mlocationrequest.setinterval(110000); mlocationrequest.setfastestinterval(55000); mlocationrequest.setpriority(locationrequest.priority_high_accuracy); } protected void startlocationupdates() { locationservices.fusedlocationapi.requestlocationupdates( mgoogleapiclient, mlocationrequest, this); } protected void doquicklocationupdate() { quicklocationrequest = new locationrequest(); quicklocationrequest.setinterval(1000); quicklocationrequest.setfastestinterval(500); quicklocationrequest.setpriority(locationrequest.priority_high_accuracy); // final argument {@code requestlocationupdates()} locationlistener // (http://developer.android.com/reference/com/google/android/gms/location/locationlistener.html). locationservices.fusedlocationapi.requestlocationupdates( mgoogleapiclient, quicklocationrequest, this); } protected void stoplocationupdates() { //todo add in onbackpresses , onstop locationservices.fusedlocationapi.removelocationupdates(mgoogleapiclient, this); } @override public void onconnected(bundle connectionhint) { log.i(tag, "connected googleapiclient"); mlastlocation = locationservices.fusedlocationapi.getlastlocation( mgoogleapiclient); if (mlastlocation != null) { starttimer(mlastlocation); } else { doquicklocationupdate(); } if (mcurrentlocation == null) { mcurrentlocation = locationservices.fusedlocationapi.getlastlocation(mgoogleapiclient); log.d(tag, "onconnected mcurrentlocation = " + mcurrentlocation.getlatitude()); } if (mrequestinglocationupdates) { startlocationupdates(); } } @override public void onlocationchanged(location location) { mcurrentlocation = location; if (quicklocationrequest != null) { quicklocationrequest = null; } if (timer == null) { starttimer(location); } log.d(tag, "onlocationchanged mcurrentlocation = " + mcurrentlocation.getlatitude()); } @override public void onconnectionsuspended(int cause) { // connection google play services lost reason. call connect() // attempt re-establish connection. log.i(tag, "connection suspended"); mgoogleapiclient.connect(); } @override public void onconnectionfailed(connectionresult result) { // refer javadoc connectionresult see error codes might returned in // onconnectionfailed. log.i(tag, "connection failed: connectionresult.geterrorcode() = " + result.geterrorcode()); } public void sendsmsmsg(string phonenumber, string message) { v.vibrate(50); // arraylist<pendingintent> sentpendingintents = new arraylist<pendingintent>(); // arraylist<pendingintent> deliveredpendingintents = new arraylist<pendingintent>(); // pendingintent sentpi = pendingintent.getbroadcast(context, 0, // new intent(context, smssentreceiver.class), 0); // pendingintent deliveredpi = pendingintent.getbroadcast(context, 0, // new intent(context, smsdeliveredreceiver.class), 0); // try { // smsmanager sms = smsmanager.getdefault(); // arraylist<string> msmsmessage = sms.dividemessage(message); // (int = 0; < msmsmessage.size(); i++) { // sentpendingintents.add(i, sentpi); // deliveredpendingintents.add(i, deliveredpi); // } // sms.sendmultiparttextmessage(phonenumber, null, msmsmessage, // sentpendingintents, deliveredpendingintents); // // } catch (exception e) { // e.printstacktrace(); // } } public void shownotification() { notificationcompat.builder mbuilder; if (build.version.sdk_int >= 16) { mbuilder = new notificationcompat.builder(context) .setsmallicon(r.mipmap.sosicon) .setsound(ringtonemanager.getdefaulturi(ringtonemanager.type_notification)) .setcontenttitle(context.getstring(r.string.app_name)) .setautocancel(true) .setcontenttext(context.getstring(r.string.notification_text)); // creates explicit intent activity in app intent resultintent = new intent(); taskstackbuilder stackbuilder = taskstackbuilder.create(context); stackbuilder.addnextintent(resultintent); pendingintent resultpendingintent = stackbuilder.getpendingintent( 0, pendingintent.flag_update_current ); mbuilder.setcontentintent(resultpendingintent); notificationmanager mnotificationmanager = (notificationmanager) context.getsystemservice(context.notification_service); mnotificationmanager.notify(10142, mbuilder.build()); } } public string getmaplink(location location) { if (location != null) { log.d(tag, "http://maps.google.com/maps?z=12&t=m&q=loc:" + location.getlatitude() + "+" + location.getlongitude()); return "http://maps.google.com/maps?z=12&t=m&q=loc:" + location.getlatitude() + "+" + location.getlongitude(); } else { return null; } } // }
and declaration in manifest:
<receiver android:name=".triggerreceiver" android:enabled="true" android:exported="true"> <intent-filter> <action android:name="packagename.mybroadcast" /> </intent-filter> </receiver>
so can wrong here , why receiver not starting?
thanks
Comments
Post a Comment