Android can';t获取要启动的服务-使用复选框启动和终止(post中的代码)
我是一个java noob,很抱歉这个noob问题 我有一个复选框,该复选框应该在选中时触发服务-在未选中时终止它。 “正在尝试…”toast按预期弹出,但服务似乎没有启动,也没有呈现toast 以下是我的mainifest条目、主活动代码(缩写)和服务代码:Android can';t获取要启动的服务-使用复选框启动和终止(post中的代码),android,android-service,android-mediarecorder,Android,Android Service,Android Mediarecorder,我是一个java noob,很抱歉这个noob问题 我有一个复选框,该复选框应该在选中时触发服务-在未选中时终止它。 “正在尝试…”toast按预期弹出,但服务似乎没有启动,也没有呈现toast 以下是我的mainifest条目、主活动代码(缩写)和服务代码: // manifest entries <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <ac
// 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();
};
};
这会在emulator上返回一个错误,表示应用程序意外退出,我单击了强制关闭,但主活动没有关闭,因此我假设我强制关闭的是服务。
以下是DDMS输出:
java.lang.RuntimeException: Unable to instantiate service com.domain.notlaunchingservice.PPS
我不在乎如何启动该服务,只要它可以加载SQL库,并在主活动失去焦点或关闭后继续将音频录制到文件中
这将是一个免费的应用程序在市场上完成后,所以你的帮助将被许多人感谢时,该项目是黄金时段准备
感谢阅读一个问题是,您用于启动和停止服务的意图只指定了一个额外的值
Intent startPPS = new Intent();
应该是
Intent startPPS = new Intent(this, PPS.class);
但是还有其他问题(我不认为您的示例已经编译),我建议您在
编辑:(下面的示例代码)
它编译了,但服务没有启动。使用第二种方法,服务似乎启动了,然后立即崩溃。哇,老兄……这个例子我想不起来了。我刚刚开始使用android编程,所以我试图通过建立到目前为止的基础来保持它的基本功能。请记住,主菜单代码只是一个片段。这可能就是您所说的“(我认为您的示例无法编译)”。扩展IntentService类是实现已启动服务的最简单、最简单的方法。不管怎样,祝你好运!
Intent startPPS = new Intent(this, PPS.class);
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));
}
});
}
}