代码如下,本应按下第二个按钮就调用service的ondestory方法,但没有,并且会强退。
activity_main.xml:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开service"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="销毁service"/>
java代码如下:
package com.example.test_service;
import android.os.Bundle;
import android.widget.*;
import android.app.Activity;
import android.view.Menu;
import android.app.Service;
import android.os.IBinder;
import android.content.Intent;
import android.view.View.OnClickListener;
import android.view.View;
public class MainActivity extends Activity {
Button but=null;
Button but2=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but=(Button)super.findViewById(R.id.button);
but2=(Button)super.findViewById(R.id.button2);
but.setOnClickListener(new OnClickListenerimpl());
but2.setOnClickListener(new OnClickListenerimpl2());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class OnClickListenerimpl implements OnClickListener
{
public void onClick(View v)
{
startService(new Intent(MainActivity.this,Test.class));
}
}
class OnClickListenerimpl2 implements OnClickListener
{
public void onClick(View v)
{
stopService(new Intent(MainActivity.this,Test.class));
}
}
}
另外该service的类如下:
package com.example.test_service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class Test extends Service
{
public IBinder onBind(Intent intent)
{
return null;
}
public void onCreate()
{
System.out.println("现在create了service");
}
public int onStartCommand(Intent intent,int flag,int startId)
{
System.out.println("现在启动service,启动的id是"+startId);
return Service.START_CONTINUATION_MASK;
}
public void onDestory()
{
System.out.println("现在销毁service");
}
}
另外,在AndroidMainfest.xml中已添加如下语句:
按下“打开service”按钮,logcat如下图:
按下“销毁service",后台并不会输出相应的信息,而是等一段时间后就会如图:
此时也会弹出提示框要强退:
请问是什么问题?应该怎么解决?
你 stopService 的时候是新建的一个 intent,应该在startService 的时候创建一个 intent 对象,然后stopService的时候释放这个对象,而不是新建一个对象
都不是同一个服务对象,瞎搞啥子嘛。
The return value from onStartCommand() must be one of the following constants:
START_NOT_STICKY
If the system kills the service after onStartCommand() returns, do not recreate the service, unless there are pending intents to deliver. This is the safest option to avoid running your service when not necessary and when your application can simply restart any unfinished jobs.
START_STICKY
If the system kills the service after onStartCommand() returns, recreate the service and call onStartCommand(), but do not redeliver the last intent. Instead, the system calls onStartCommand() with a null intent, unless there were pending intents to start the service, in which case, those intents are delivered. This is suitable for media players (or similar services) that are not executing commands, but running indefinitely and waiting for a job.
START_REDELIVER_INTENT
If the system kills the service after onStartCommand() returns, recreate the service and call onStartCommand() with the last intent that was delivered to the service. Any pending intents are delivered in turn. This is suitable for services that are actively performing a job that should be immediately resumed, such as downloading a file.
你在oncreat()方法里面声明 Intent intent=new Intent(Mainactivity.this,test.class);
然后 分别在按钮监听里 startService(intent);
stopService(intent);
如下 :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but=(Button)super.findViewById(R.id.button);
but2=(Button)super.findViewById(R.id.button2);
but.setOnClickListener(new OnClickListenerimpl());
but2.setOnClickListener(new OnClickListenerimpl2());
Intent intent=new Intent(Mainactivity.this,test.class);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class OnClickListenerimpl implements OnClickListener
{
public void onClick(View v)
{
startService(intent);
}
}
class OnClickListenerimpl2 implements OnClickListener
{
public void onClick(View v)
{
stopService(intent);
}
}
class OnClickListenerimpl implements OnClickListener
{
public void onClick(View v)
{
startService(new Intent(MainActivity.this,Test.class));
}
}
class OnClickListenerimpl2 implements OnClickListener
{
public void onClick(View v)
{
stopService(new Intent(MainActivity.this,Test.class));
}
}
把这两个合在一起:监听同一个OnClickListenerimpl
class OnClickListenerimpl implements OnClickListener
{
public void onClick(View v)
{
Intent intent=new Intent(MainActivity.this,Test.class);
switch(v.getId()){
case R.id.button:
startService(intent);
break;
case R.id.button2:
stopService(intent);
break;
}
}
}