我希望我的应用程序能够进行协议,是否可以连接到Web以及当前GSM连接的信号强度。我想在每一分钟重复这个过程。
此时我的班级看起来像:
public class UmtsActivity extends Activity {
[...]
TelephonyManager Tel;
MyPhoneStateListener MyListener;
int currentSignalStrength;
[...]
private class MyPhoneStateListener extends PhoneStateListener
{
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength)
{
super.onSignalStrengthsChanged(signalStrength);
currentSignalStrength = signalStrength.getGsmSignalStrength();
}
到目前为止,我只使用phonestateListener的onSignalStrengthsChanged()函数读取SignalStrength。还有其他方法,所以我可以节省CPU时间吗?
诚恳 Wolfen的
答案 0 :(得分:1)
如果您将PhoneStateListener作为服务的一部分,那么您的应用程序组件始终知道当前的信号强度。然后,应用程序的UI部分可以根据需要查询服务状态,或者在强度变化超出某个限制时发布更新。例如,信号强度会迅速变化 - 因此只有在更新超过10%时才发布更新,或者按时间限制更新。
例如(此处更新仅发送感兴趣的对象被更改(mSms或mMins),并且只有每5个信号强度发生变化。应用程序的活动将接收此Intent。这由服务调用&# 39; s PhoneStateListener重写方法。
/* called from the phone state listener to check for any changes in the number of sms/calls since last time
* and to raise a suitable intent if required. Check the time to avoid calling too fast.
*/
private void update()
{
final String key = getString(R.string.pref_periodstart_key);
final String defVal = getString(R.string.periodstart_defaultValue);
Thread thread = new Thread(null, new Runnable() {
public void run() {
Context context = getApplicationContext();
int start = Integer.valueOf(mPrefs.getString(key, defVal));
Date date = PhoneUsageInfo.getLastContractDate(start);
int mins = PhoneUsageInfo.getMinutesUsedSince(context, date);
int sms = PhoneUsageInfo.getSMSCountSince(context, date);
boolean changed = (mins != mMins || sms != mSms);
if (changed || mCount % 5 == 0) {
mMins = mins;
mSms = sms;
Intent intent = new Intent(Constants.INTENT_USAGE_UPDATE);
intent.putExtra(Constants.EXTRA_SMS_COUNT, sms);
intent.putExtra(Constants.EXTRA_MIN_COUNT, mins);
intent.putExtra(Constants.EXTRA_START_DATE, date);
Log.d(TAG, String.format("service sending broadcast intent sms %d, mins %d (%s)", sms, mins, changed?"changed":"unchanged"));
sendBroadcast(intent);
}
mCount += 1;
}
}, "PhoneUsageServiceUpdate");
thread.start();
}