我是Android App Development的新手,并创建了一个简单的服务应用。它有一个启动服务的按钮和一个按钮来停止服务的各种方法。以下是我的代码:
App3_main.java
package eg.app3;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class App3_main extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app3_main);
}
public void startservice(View view)
{
Intent intent = new Intent(this,MyService.class);
startservice(intent); //this is where I'm getting the error mentioned in the title
}
public void stopservice(View view)
{
Intent intent = new Intent(this,MyService.class);
stopservice(intent); //this is where I'm getting the error mentioned in the title
}
}
MyService.java
package eg.app3;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
public MyService() {
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this,"Service Started",Toast.LENGTH_LONG).show();
return START_STICKY;
}
@Override
public void onDestroy() {
Toast.makeText(this,"Service Stopped",Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
请指导我错在哪里。
答案 0 :(得分:0)
替换:
startservice(intent);
使用:
startService(intent);
然后,替换:
stopservice(intent);
使用:
stopService(intent);
与大多数编程语言一样,Java区分大小写。