Java 在Android中设置要从按钮调用的服务
我有这些按钮可以工作:Java 在Android中设置要从按钮调用的服务,java,android,service,Java,Android,Service,我有这些按钮可以工作: public void onClick(View v) { if (v.getId() == R.id.buttonPlay) { buttonStopPlay.setBackgroundResource(R.drawable.stopbutton); buttonPlay.setBackgroundResource(R.drawable.playbuttonpressed); Context context = g
public void onClick(View v) {
if (v.getId() == R.id.buttonPlay) {
buttonStopPlay.setBackgroundResource(R.drawable.stopbutton);
buttonPlay.setBackgroundResource(R.drawable.playbuttonpressed);
Context context = getApplicationContext();
CharSequence text = "Buffering...";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.setGravity(Gravity.BOTTOM, 0, 200);
ViewGroup group = (ViewGroup) toast.getView();
TextView messageTextView = (TextView) group.getChildAt(0);
messageTextView.setTextSize(40);
toast.show();
startPlaying();
} else if (v.getId()==R.id.buttonStopPlay) {
buttonPlay.setBackgroundResource(R.drawable.playbutton);
buttonStopPlay.setBackgroundResource(R.drawable.stopbuttonpressed);
stopPlaying();
}
}
我想设置一个简单的服务来保持startPlaying()代码>当应用程序最小化时,我这样做:
public class BgMusicService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startPlaying();
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
public void onClick(View v) {
if (v.getId() == R.id.buttonPlay) {
buttonStopPlay.setBackgroundResource(R.drawable.stopbutton);
buttonPlay.setBackgroundResource(R.drawable.playbuttonpressed);
Context context = getApplicationContext();
CharSequence text = "Buffering...";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.setGravity(Gravity.BOTTOM, 0, 200);
ViewGroup group = (ViewGroup) toast.getView();
TextView messageTextView = (TextView) group.getChildAt(0);
messageTextView.setTextSize(40);
toast.show();
Intent intent = new Intent(this, BgMusicService.class);
startService(intent);
} else if (v.getId()==R.id.buttonStopPlay) {
buttonPlay.setBackgroundResource(R.drawable.playbutton);
buttonStopPlay.setBackgroundResource(R.drawable.stopbuttonpressed);
Intent intent = new Intent(this, BgMusicService.class);
stopService(intent);
stopPlaying();
}
}
然而,服务不起作用。我做错了什么吗?将onStartCommand()
覆盖到您的服务中,并在其中调用您的startPlaying()
方法。此外,您还需要对服务
使用长后台操作,而不是IntentService
<代码>目的价格
在操作结束后将被终止。Da,执行持续性的连续性交叉连接终端、pe c和非目的服务,在交叉连接终端。Cred ca nu ar fi trebuit sa ti raspund in romana,ci in engleza.您是否将服务声明添加到清单中?请使用
而不是$
Y知道,使用点,而不是美元符号。您收到该警告,因为包必须以小写开头,只有类必须以大写开头,因为包名都是用小写写的,以避免与类或接口的名称冲突。