2012-06-30 7 views
1

아마도 여러분에게 쉬운 질문 일 것입니다.하지만 내 앱이 닫힐 때 백그라운드에서 실행되어야하는 서비스를 만드는 첫 번째 시도입니다. 내 문제는 : "시작 서비스"버튼을 클릭하면 서비스가 시작되지 않습니다. 나는 어떤 축배도 보지 않으며, 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> 
+0

context.startService를 사용해 보셨습니까? 이 시작됩니다. 서비스 로그가 생성되는 중입니다. – AAnkit

+0

@AnkitAwasthi 성공하지 않고 지금 시도해보십시오. 그리고 예, 로그에서 "onclick, starting service"를 얻습니다. 그러나 서비스에서 아무것도 : ../ – Johan

+0

나는 대답이 당신의 문제를 해결할 수있는 게시 생각합니다. 활동의 전체 코드를 게시하지 않으면 내 설정을 시도합니다 .. – AAnkit

답변

4
this is suggestion as all else looking fine 
서비스가 이미 시작을 할 수 중지하고 다시 시작하고 토스트가 나타나면 확인을 시도 할 수 있습니다로

시도 .... super.onCreate();

같이 각 기능에서 슈퍼를 호출해야

<service android:enabled="true" android:name="se.johanberntsson.servies.MyService" /> 
+1

http : //mobile.dzone.com/articles/android-services –

+1

행운을 빌어 요 ... 그게 꼭 필요한가요? http://marakana.com/forums/android/examples/60.html – Johan

+0

이것은 다른 모든 사람들이 잘 보이는 것임을 의미합니다. –

0

내 코드에 서비스를 추가했는데 제대로 작동합니다. 아래 코드를 참조하여 자신과 일치하십시오.

활동 >>

public class TestActivity extends Activity implements OnClickListener { 
    /** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    Button bt = (Button)findViewById(R.id.button11); 
    bt.setOnClickListener(this); 
} 


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)); 
} 

@Override 
public void onClick(View v) { 
    startServiceClick(v); 

} 

매니페스트 파일 >>

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.test" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk android:minSdkVersion="8" /> 

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 
    <activity 
     android:name=".TestActivity" 
     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=".MyService" /> 

</application> 

+0

맞습니다. 나는 onclicklistener를 사용하는 것을 제외하고 xml의'onClick' 속성 만 사용합니다. 그게 문제가 아니야 – Johan

+0

오오 .. 그럼, onclicklistener를 추가해보십시오. 안드로이드는 아직 성숙하지 않았습니다 ... 당신의 코드에 어리석은 일이있을 수 있습니다. – AAnkit

+0

내 서비스가 다른 패키지에 있다고 생각할 수도 있습니다. 'se.johanberntsson.services'? – Johan

1

당신은 이미 알아 냈 수 있습니다. 코드에서 토스트와 catlog 항목을 추가 할 수 있도록 몇 가지 기본 함수를 재정의합니다. 슈퍼를 추가하는 것을 잊지 마십시오. {당신이 부르는 기능}. 그것이하는 일은 기능이 오버라이드되지 않은 상태에서 수행 한 모든 작업을 안드로이드 장치에 지시 한 다음 여분의 작업을 수행하는 것입니다. 귀하의 경우에는 oncreate 함수를 호출하면 컴퓨터가 축배를 만들고 catlog 항목을 추가하지만 서비스를 생성하지는 않습니다.

@Override //forget about doing whatever onCreate usually does. 
     public void onCreate() { 
         Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();//toast 
      Log.d(TAG, "onCreate");//Add message to cat- log 
      //note: Service not created because function was overrided 
     } 

그러나 super.onCreate를 추가하면 전화가 서비스를 생성 한 다음 사용자가 수행 할 작업을 수행합니다.

이 정보가 도움이되기를 바랍니다.

관련 문제