帮看一下这个服务后台不运行 , 过一段时间会自动销毁服务

风影楼c 2019-06-02 01:18:08
package com.zzy.eqclient.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.telephony.CellLocation;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;

import com.zzy.eqclient.util.ShareUtil;

public class StrengthService extends Service {

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate() {
super.onCreate();
final TelephonyManager telephonyManager = (TelephonyManager)getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(new MyPhoneListener(), PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}

@Override
public void onDestroy() {
super.onDestroy();
System.out.println("服务被销毁了……");
}

// @Override
// public int onStartCommand(Intent intent, int flags, int startId) {
// return super.onStartCommand(intent, flags, startId);
// }

private class MyPhoneListener extends PhoneStateListener {
@Override
public void onCellLocationChanged(CellLocation location) {
if (location instanceof GsmCellLocation) {
int cid = ((GsmCellLocation) location).getCid();
Log.i("获取到CID:", cid + "");
ShareUtil.putInt(getApplicationContext(), "cid", cid);
} else if (location instanceof CdmaCellLocation) {
int id = ((CdmaCellLocation) location).getBaseStationId();
Log.i("获取到ID:", id + "");
ShareUtil.putInt(getApplicationContext(), "id", id);
}
}

@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
//这个ltedbm 是4G信号的值
String signalinfo = signalStrength.toString();
String[] parts = signalinfo.split(" ");
int dbm = Integer.parseInt(parts[9]);
int asu = 140 + dbm;
if(asu == 0) {
//这个dbm 是2G和3G信号的值
asu = signalStrength.getGsmSignalStrength();
dbm = -113 + 2 * asu;
}
ShareUtil.putInt(getApplicationContext(), "dbm", dbm);
ShareUtil.putInt(getApplicationContext(), "asu", asu);
Log.i("获取到DBM:", dbm + "\n" + "获取到ASU:" + asu + "");
super.onSignalStrengthsChanged(signalStrength);
}
}
}


开着activity的时候,服务正常运行,程序切到后台就不会继续打印日志了,为什么呢
...全文
80 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
风影楼c 2019-06-02
  • 打赏
  • 举报
回复
有人吗,试了用动态广播也不行啊

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧