I know that this question has been asked many times, but all other topics did not solve my problem at all, I do not see anything bad in my code. Maybe I missed something, can someone help me?
Code for Intent Service:
package Services; import android.app.IntentService; import android.content.Intent; import android.widget.Toast; public class WifiSearchService extends IntentService { public WifiSearchService() { super("WifiSearchService"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show(); return super.onStartCommand(intent,flags,startId); } @Override protected void onHandleIntent(Intent intent) {
Start the service by clicking the switch:
package com.cdobiz.wifimapper; import Services.WifiSearchService; import android.os.Bundle; import android.app.Activity; import android.app.ActivityManager; import android.app.ActivityManager.RunningServiceInfo; import android.content.Context; import android.content.Intent; import android.util.Log; import android.view.Menu; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.Switch; public class MainActivity extends Activity { private Context context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); context = this; setContentView(R.layout.activity_main); Switch serviceSwitch = (Switch) this.findViewById(R.id.switchService); serviceSwitch.setChecked(isMyServiceRunning()); serviceSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener(){ @Override public void onCheckedChanged(CompoundButton view, boolean state) { if(state){ startService(new Intent(context, WifiSearchService.class)); }else{ stopService(new Intent(context, WifiSearchService.class)); } } }); } private boolean isMyServiceRunning() { ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE); for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { Log.v("debug", service.service.getClassName()); if ("com.cdobiz.wifimapper.services.WifiSearchService".equals(service.service.getClassName())) { return true; } } return false; } @Override public boolean onCreateOptionsMenu(Menu menu) {
manifest:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cdobiz.wifimapper" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.cdobiz.wifimapper.MainActivity" 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:enabled="true" android:name=".services.WifiSearchService"></service> </application> </manifest>
thedjaney
source share