Nerozumim proc se mi servasa neustale restartuje
v
public class AppReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//ToneGenerator tg=new ToneGenerator(AudioManager.STREAM_MUSIC, 100);
//tg.startTone(ToneGenerator.TONE_DTMF_1, 30);
Toast.makeText(context, "OnReceive", Toast.LENGTH_LONG).show();
if(FirstStart(intent)) { // start po instalaci a rebootu mobila
Toast.makeText(context,"Pred start service",Toast.LENGTH_LONG).show();
context.startService(new Intent(context, GPSService.class));// toto je podstany radek-volani
...
}
proste ta servisa se nastartuje potud OK - dalsi kus kodu -tudy to urcite proleti
public class GPSService extends IntentService { //zkousel jsme tridu service
public int onStartCommand(Intent intent, int flags, int startId) {
//tg.startTone(ToneGenerator.TONE_DTMF_3, 30);
Toast.makeText(getApplicationContext(), "onStartCommand", Toast.LENGTH_LONG).show();
if (!td.isAlive()) {
td.stop=false;
td.run();
Toast.makeText(getApplicationContext(), "onStartCommand - Start", Toast.LENGTH_LONG).show();
}
return super.onStartCommand(intent, flags, startId);
}
vlastni kod te servisy
private class ThreadDemo extends Thread {
public boolean stop;
LocationManager locationlistener=null;
@Override
public void run() {
Toast.makeText(getApplicationContext(), "onStartCommand - Run", Toast.LENGTH_LONG).show();
stop = false;
while (!stop) {
// tady bych potreboval vykony code
}
Toast.makeText(getApplicationContext(), "onStartCommand - End", Toast.LENGTH_LONG).show();
super.run();
}
takze bez ohledu na promenou STOP to hned vleti do onDestroy(....
@Override
public void onDestroy() {
Toast.makeText(getApplicationContext(), "onDestroy", Toast.LENGTH_LONG).show();
td.stop=true;
super.onDestroy();
}
manifest je takto nastaven - ta activita neni dulezita (alespon ted hned) mozna ji nekdy vyuziju pouze pro statistycka info o behu programu
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cz.soft.zektel.gpschecker">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".gpsCheckerActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".GPSService"
android:enabled="true"
android:exported="true"></service>
<receiver
android:name=".AppReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.REBOOT" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.PACKAGE_FIRST_LAUNCH" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<!--<action android:name="com.android.vending.INSTALL_REFERRER" />-->
</intent-filter>
</receiver>
</application>
</manifest>
Tak kdyz tu servisu zpustim z te aktivity tak vse funguje OK
kdyz ale spustim z toho BroadcastReceiveru tak se nekolikrat zrestartuje a pak chcipne
tusim ze ja nevim jak spravne pracovat s androidi servisou - a fakt jsem googlil ja smyslu zbaveny
program by mel bezet Android 3.0 a vyse