아마도 여러분에게 쉬운 질문 일 것입니다.하지만 내 앱이 닫힐 때 백그라운드에서 실행되어야하는 서비스를 만드는 첫 번째 시도입니다. 내 문제는 : "시작 서비스"버튼을 클릭하면 서비스가 시작되지 않습니다. 나는 어떤 축배도 보지 않으며, logcat에는 아무 것도 보이지 않는다 (아무런 오류도 없다). 미리 감사드립니다!서비스가 시작되지 않음
서비스 클래스
public class MyService extends Service {
private static final String TAG = "MyService";
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
}
@Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
}
}
내 주요 활동
public void startServiceClick(View v){
Log.d("foo", "onClick: starting srvice");
startService(new Intent(this, MyService.class));
}
public void stopServiceClick(View v){
Log.d("foo", "onClick: stopping srvice");
stopService(new Intent(this, MyService.class));
}
매니페스트 XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="se.johanberntsson.main"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<application
android:icon="@drawable/maps1"
android:label="@string/app_name" >
<uses-library android:name="com.google.android.maps" />
<activity
android:name=".LongitudeActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DebugActivity" />
<activity android:name=".GoogleMapsActivity" />
<service android:enabled="true" android:name=".MyService" />
</application>
</manifest>
context.startService를 사용해 보셨습니까? 이 시작됩니다. 서비스 로그가 생성되는 중입니다. – AAnkit
@AnkitAwasthi 성공하지 않고 지금 시도해보십시오. 그리고 예, 로그에서 "onclick, starting service"를 얻습니다. 그러나 서비스에서 아무것도 : ../ – Johan
나는 대답이 당신의 문제를 해결할 수있는 게시 생각합니다. 활동의 전체 코드를 게시하지 않으면 내 설정을 시도합니다 .. – AAnkit