gpt4 book ai didi

android - stopService() 方法不停止服务

转载 作者:搜寻专家 更新时间:2023-11-01 07:55:30 25 4
gpt4 key购买 nike

当调用 Activity 退出时,我从调用类调用了 stopService() 方法,服务没有停止。它一直在后台运行,因此可能会给最终用户带来麻烦。任何帮助将不胜感激..

代码:

public class MainActivity2 extends ActionBarActivity {
ListView listView;
static int cnt1=0;
static int bob=1;
RelativeLayout background2;


public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main_activity2);



background2 = (RelativeLayout) findViewById(R.id.background);
int num = 0;
final ListView theListView = (ListView) findViewById(R.id.listView);
Intent calledActivity = getIntent();
final List pe = calledActivity.getExtras().getStringArrayList("Caller1");

String[] s = new String[pe.size()];
for (int i = 0; i < pe.size(); i++) {
s[i] = (String) pe.get(i);
}

final ListAdapter theAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, pe);
theListView.setAdapter(theAdapter);
final int cnt = 1;
Intent mIntent = new Intent(this, MyService.class);
mIntent.putStringArrayListExtra("Caller2", (ArrayList<String>) pe);
mIntent.putExtra("intCall",bob);
startService( mIntent);



theListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String s1 = String.valueOf(parent.getItemAtPosition(position));
pe.remove(s1);
if (pe.size() == 0) {
stopService(new Intent(MainActivity2.this, MyService.class));

Intent goback;
goback = new Intent(MainActivity2.this, MainActivity.class);
startActivity(goback);
}
((BaseAdapter) theAdapter).notifyDataSetChanged();


}
});
}

我的服务等级:

public class MyService extends Service {
public MyService() {
}

@Override
public IBinder onBind(Intent arg0) {

return null;}

@Override
public void onCreate() {

Toast.makeText(this, "Congrats! Blocker Created", Toast.LENGTH_LONG).show();

}

@Override
public void onStart(final Intent intent, int startId) {
int i=0;

Timer t = new Timer();

t.scheduleAtFixedRate(new TimerTask() {

@Override
public void run() {
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(1);


ActivityManager.RunningTaskInfo ar = tasks.get(0);
String activityOnTop=ar.topActivity.getClassName();
Intent lockIntent = new Intent(MyService.this, LockScreen.class);
lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ComponentName componentInfo = tasks.get(0).topActivity;
if(!componentInfo.getPackageName().equals("com.example.epiclapser.noprocrastinate"))
{

startActivity(lockIntent);
Log.v("iwashere", "-- ACTIVITY --");
}
}


},
0,

1000);

Toast.makeText(this, "My Blocker Started", Toast.LENGTH_LONG).show();

}

@Override
public void onDestroy() {
Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show();
super.onDestroy();
}


}

最佳答案

您的服务中的 onStart() 是一个已弃用的方法,您应该改用 onStartCommand。请参阅此链接 here .它还指出,默认情况下 onStart 会设置“START_STICKY”标志,这会导致服务在被终止后自动重新启动。我可以想象这会导致您的问题。尝试 OnStartCommand() 并让它返回“START_NOT_STICKY”标志。祝你好运!

关于android - stopService() 方法不停止服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28275551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com