2012-02-09 1 views
0

(POST 코드) ...는 서비스가 시작되지 수 - 내가하기로되어있는 체크 박스를 가지고 내가, 멍청한 놈 질문에 대한 유감 자바 멍청한 놈이야

을 시작하고 체크 박스로 사망 점검 할 때 서비스를 끄십시오 - 선택하지 않으면 서비스를 종료하십시오. "시도 중 ..."토스트가 예상대로 튀어 나오지만 서비스가 시작되지 않은 것으로 보이고 토스트가 표시되지 않습니다.

여기 내 주요 항목, 기본 활동 코드 (요약) 및 서비스 코드 :

// manifest entries 

<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > 
<activity android:name=".MainMenu" 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=".PPS" android:process=":remote" android:label="@string/service_name" /> 
</application> 

// MainMenu.java 

final CheckBox checkBoxStartService = (CheckBox) findViewById(R.id.checkBoxEnable); 
checkBoxStartService.setOnCheckedChangeListener(new OnCheckedChangeListener() 
{ 
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
{ 
    if (checkBoxStartService.isChecked() == true) 
    {  // make toast 
    Toast toaster = Toast.makeText(MainMenu.this, "attempting to start service...", 500); 
    toaster.setGravity(Gravity.CENTER, 0, -200); 
    toaster.show(); 
    Intent startPPS = new Intent(); 
    startPPS.putExtra("com.domain.notlaunchingservice.PPS", false); 
    startService(startPPS); 
    } 
    if (checkBoxStartService.isChecked() == false) 
    { 
    Intent closePPS = new Intent(); 
    closePPS.putExtra("com.domain.notlaunchingservice.PPS", true); 
    stopService(closePPS); 
    } 
} 
}; 

// PPS.java 

package com.domain.notlaunchingservice; 
import android.app.Service; 
import android.view.Gravity; 
import android.widget.Toast; 
public abstract class PPS extends Service 
{ 
@Override 
public void onCreate() 
{ 
    Toast toasty = Toast.makeText(PPS.this, "service created!", 500); 
    toasty.setGravity(Gravity.CENTER, 0, 200); 
    toasty.show(); 
}; 
public void onDestroy() 
{ 
    Toast toasted = Toast.makeText(PPS.this, "service destroyed!", 500); 
    toasted.setGravity(Gravity.CENTER, 0, -200); 
    toasted.show(); 
}; 
}; 

는 나는 또한 사용하여 서비스를 호출하는 것을 시도했다 :

startService(new Intent(MainMenu.this, PPS.class)); 

이 앱이 예기치 않게 종료하고 난 힘 닫기를 클릭 말하는 에뮬레이터에서 오류를 반환하지만 주요 활동은 '아무튼 t 닫기, 그래서 나는 그것이 servi라고 가정하고있다. 그게 내가 강제로 닫는 거지. 내가 서비스를 시작하는 방법을 주요 활동 포커스를 잃을 또는 후에 그것은 SQL 기반을로드하고 파일에 기록 오디오를 계속할 수

java.lang.RuntimeException: Unable to instantiate service com.domain.notlaunchingservice.PPS 

내가 한 상관 없어 : 아래 DDMS의 출력 닫은.

완료되면 시장에서 무료로 제공되는 앱이므로 프로젝트가 황금 시간대에 준비되면 많은 도움을받을 수 있습니다.

답변

0

하나의 문제를 읽기위한

감사를 시작하고 서비스를 중지하는 데 사용하는 의도는 여분을 지정하는 것입니다.

Intent startPPS = new Intent(); 

해야

Intent startPPS = new Intent(this, PPS.class); 

그러나 다른 문제는 (내가 당신의 예를 컴파일 생각하지 않는다), 내가 http://developer.android.com/guide/topics/fundamentals/services.html#ExtendingIntentService

편집의 예를 살펴 당신을 추천 할 것입니다뿐만 아니라이 있습니다 : (아래 예제 코드)

public class StartedService extends Service { 

    @Override 
    public IBinder onBind(Intent intent) { 
     // I guess that you don't want to bind to the service 
     return null; 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Toast.makeText(this, "Service is running", Toast.LENGTH_SHORT).show(); 
     // Return sticky if you want it to be restarted in a low memory 
     // situation 
     return START_STICKY; 
    } 

    @Override 
    public void onDestroy() { 
     Toast.makeText(this, "Service is going down", Toast.LENGTH_SHORT).show(); 
    } 
} 


public class ClientActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Button startButton = (Button)findViewById(R.id.start_button); 
     startButton.setOnClickListener(new OnClickListener() { 

      public void onClick(View view) { 
       startService(new Intent(ClientActivity.this, StartedService.class)); 
      } 

     }); 

     Button stopButton = (Button)findViewById(R.id.stop_button); 
     stopButton.setOnClickListener(new OnClickListener() { 

      public void onClick(View view) { 
       stopService(new Intent(ClientActivity.this, StartedService.class)); 
      } 

     }); 
    } 
} 
+0

컴파일되지만 서비스가 시작되지 않습니다. – notgoodwithjava

+0

두 번째 방법을 사용하면 서비스가 시작된 것처럼 보입니다. – notgoodwithjava

+0

링크를 이용해 주셔서 감사합니다. 지금 읽어 보겠습니다. – notgoodwithjava

관련 문제