java - Android - Service does not start -
i making app me , class mates, checks new homeworks , exams. has service may not killed beacuse of checking school server (like every 5 minutes) changed size of file list of exams , stuff this. tried following code:
mayinactivity.java
package com.test.simpleservice; import android.app.activitymanager; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.button; import java.util.list; public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //checking runings services - remove later activitymanager = (activitymanager)this.getsystemservice(activity_service); list<activitymanager.runningserviceinfo> rs = am.getrunningservices(50); string message = null; (int i=0; i<rs.size(); i++) { activitymanager.runningserviceinfo rsi = rs.get(i); system.out.println("!!!!!!!!!!service" + "process " + rsi.process + " component " + rsi.service.getclassname()); message = message + rsi.process; } button btnstart = (button) findviewbyid(r.id.startbtn); btnstart.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { servicemanager.startservice(getapplicationcontext()); } }); } }
myservices.java
package com.test.simpleservice; import android.app.service; import android.content.intent; import android.os.ibinder; import android.support.annotation.nullable; import android.widget.toast; public class myservices extends service { private static final string logtag = "myservices"; @override public void onstart(intent intent, int startid) { system.out.println("start..."); //some code of service starting,such establish connection,create timertask or else toast.maketext(this, "service start", toast.length_long).show(); } @nullable @override public ibinder onbind(intent arg0) { // todo auto-generated method stub return null; } @override public int onstartcommand(intent intent, int flags, int startid) { //this start service system.out.println("startcommand..."); toast.maketext(this, "service startcomand", toast.length_long).show(); return start_sticky; } @override public void ondestroy() { //this not kill service //super.ondestroy(); toast.maketext(this, "task destroyed", toast.length_long).show(); intent in = new intent(); in.setaction("preventkilling"); sendbroadcast(in); } @override public void ontaskremoved(intent intent){ toast.maketext(this, "task removed", toast.length_long).show(); intent = new intent(this, this.getclass()); startservice(intent); } }
reciever.java
package com.test.simpleservice; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.util.log; public class receiver extends broadcastreceiver { private static final string logtag = "receiver"; public void onreceive(context context, intent intent) { if (intent.action_boot_completed.equals(intent.getaction())) { intent serviceintent = new intent(context, myservices.class); context.startservice(serviceintent); } log.d(logtag, "servicedestroy onreceive..."); log.d(logtag, "action:" + intent.getaction()); log.d(logtag, "servicedestroy auto start service..."); servicemanager.startservice(context); } }
servicemanager
package com.test.simpleservice; import android.content.context; import android.content.intent; import android.util.log; public class servicemanager { private static final string logtag = "servicemanager"; public static void startservice(context context) { log.i(logtag, "servicemanager.startserivce()..."); intent intent = new intent(myservices.class.getname()); intent.setpackage("com.test.simpleservice"); context.startservice(intent); } }
androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.test.simpleservice"> <uses-permission android:name="android.permission.receive_boot_completed" /> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsrtl="true" android:theme="@style/apptheme"> <activity android:name=".mainactivity"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <service android:name=".myservices" /> <receiver android:name=".receiver" > <intent-filter> <action android:name="preventkilling" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.recieve_boot_completed" /> <action android:name="android.intent.action.quickboot_poweron" /> <category android:name="android.intent.category.default" /> </intent-filter> </receiver> </application> </manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.test.simpleservice.mainactivity"> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/start_btn" android:id="@+id/startbtn" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" /> </relativelayout>
when run app , click button "start", service wont start - no toasts, no logs, nothing in setting->apps->services. why? better way make unkillable process without anoying notification?
logcat last few lines becouse before listed services:
01-16 14:10:15.567 13753-13772/com.test.simpleservice i/openglrenderer: initialized egl, version 1.4 01-16 14:10:15.567 13753-13772/com.test.simpleservice w/openglrenderer: failed choose config egl_swap_behavior_preserved, retrying without... 01-16 14:10:15.587 13753-13772/com.test.simpleservice d/openglrenderer: enabling debug mode 0 01-16 14:10:17.597 13753-13753/com.test.simpleservice i/servicemanager: servicemanager.startserivce()...
and 1 more question: app start after boot? if not, did wrong?
im sorry poor english
intent intent = new intent(context, myservices.class); intent.setpackage("com.test.simpleservice"); context.startservice(intent);
because context is:
public static void startservice(context context)
and here :
public void onreceive(context context, intent intent)
for above intent
name -> intent
Comments
Post a Comment