broadcast - How to make my service run? -
i want start service when power connected device , show notification app doing nothing or not happenig, please me, new in android world, maybe miss something, mi code following:
my manifest:
<receiver android:name="com.tlm.grhs_client.wifimonitor"> <intent-filter> <action android:name="android.intent.action.action_power_connected"/> </intent-filter> </receiver>
my broadcast reciever:
public class wifimonitor extends broadcastreceiver { @override public void onreceive(context context, intent intent) { connectivitymanager cm = (connectivitymanager) context.getsystemservice(context.connectivity_service); networkinfo info = cm.getactivenetworkinfo(); if (info != null && info.gettype() == connectivitymanager.type_wifi) { if (info.isconnected()) { //iniciar servicio intent serviceintent = new intent(context, clientservice.class); context.startservice(serviceintent); } else { //parar servicio intent serviceintent = new intent(context, clientservice.class); context.stopservice(serviceintent); } } } }
and service class this:
public class clientservice extends service { @override public ibinder onbind(intent intent) { throw new unsupportedoperationexception("not yet implemented"); } @override public void oncreate() { toast.maketext(this, "servicio fue creado", toast.length_long).show(); } @targetapi(build.version_codes.jelly_bean) @suppresslint("newapi") @override public void onstart(intent intent, int startid) { // perform long running operations here. toast.maketext(this, "servicio iniciado", toast.length_long).show(); notification.builder notibuilder = new notification.builder(this); notibuilder.setcontenttitle("prueba"); notibuilder.setcontenttext("esto es una prueba"); notification notificacion = notibuilder.build(); notificationmanager manager = (notificationmanager) this.getsystemservice(this.notification_service); manager.notify(1, notificacion); } @override public void ondestroy() { toast.maketext(this, "servicio destruido", toast.length_long).show(); } }
i turn on wifi , establish connection notification doesn't shows.
android notifications in manner require small icon, along title , text per documentation @ http://developer.android.com/guide/topics/ui/notifiers/notifications.html
add icon otherwise code (on quick glance) looks should work.
Comments
Post a Comment