0

我是一个java菜鸟,很抱歉这个菜鸟问题......

我有一个复选框,它应该在选中时触发服务-未选中时将其杀死。“尝试...”吐司按预期弹出,但服务似乎没有启动,也没有出现吐司..

这是我的主要条目、主要活动代码(缩写)和服务代码:

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

这会在模拟器上返回一个错误,说应用程序意外退出并且我单击强制关闭,但主要活动没有关闭,所以我假设它是我强制关闭的服务。下面是 DDMS 输出:

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

我不在乎我如何启动服务,只要它可以加载 SQL 库并在主要活动失去焦点或关闭后继续将音频录制到文件中。

完成后,这将是市场上的免费应用程序,因此当项目准备好进入黄金时段时,许多人会感谢您的帮助。

谢谢阅读

4

1 回答 1

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

        });
    }
}
于 2012-02-09T10:19:16.163 回答