无法启动服务 - 使用复选框启动和终止(帖子中的代码)

发布于 2025-01-03 17:55:19 字数 2457 浏览 1 评论 0原文

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

我有一个复选框,应该在选中时启动服务 - 未选中时杀死它。 “尝试...” toast 按预期弹出,但服务似乎没有启动并且不显示 toast..

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

// 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 库并在主活动失去焦点或关闭后继续将音频记录到文件中即可。

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

感谢您的阅读

i'm a java noob, so sorry for the noob question...

i have a checkbox that is supposed to fire off a service when checked - kill it when unchecked.
the "attempting..." toast pops up as expected, but the services does not seem to be starting and does not present toast..

here are my mainifest entries, main activity code (abbridged), and the service code:

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

i have also tried to call the service using:

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

this returns an error on the emulator saying the app quit unexpectedly and i click force close, but the main activity doesn't close, so i'm assuming it's the service that i am force closing.
below is the DDMS output:

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

i don't care how i get the service started, as long as it can load a SQL base and continue recording audio to a file after the main activity loses focus or is closed.

this will be a free app on the market when finished, so your help will be appreciated by many when the project is ready for prime time.

thanks for reading

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

手长情犹 2025-01-10 17:55:19

一个问题是您用来启动和停止服务的意图仅指定了一个额外的。

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

        });
    }
}

One problem is that the intent you use to start and stop the service only specifies an extra.

Intent startPPS = new Intent();

should be

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

But there are other problems as well (I don't think your example compiles), I would recommend you to look at the example at http://developer.android.com/guide/topics/fundamentals/services.html#ExtendingIntentService

Edit: (example code below)

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

        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文