我是一个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 库并在主要活动失去焦点或关闭后继续将音频录制到文件中。
完成后,这将是市场上的免费应用程序,因此当项目准备好进入黄金时段时,许多人会感谢您的帮助。
谢谢阅读