无法启动服务 - 使用复选框启动和终止(帖子中的代码)
我是一个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个问题是您用来启动和停止服务的意图仅指定了一个额外的。
应该是
但是还有其他问题(我认为你的示例无法编译),我建议你查看 http://developer.android.com/guide/topics/fundamentals/services.html#ExtendingIntentService
编辑:(下面的示例代码)
One problem is that the intent you use to start and stop the service only specifies an extra.
should be
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)