ANDROID 使用 Service 在手机锁屏休眠状态下后台执行发送短信息回调HANDLE界面显示内容
16lz
2022-04-21
MainActivity.javapublic class MainActivity extends Activity implements ServiceConnection { private TextView netStateText; private TextView sendedText; private TextView waitingText; private TextView lastFinishTimeText; SmsApplication app; boolean checkMoveTaskToBack = false; private PowerManager.WakeLock wakeLock = null; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); try { setTitle(getString(R.string.app_name)); setContentView(R.layout.activity_main); Bundle bundle = getIntent().getExtras(); checkMoveTaskToBack = bundle.getBoolean("checkMoveTaskToBack"); app = (SmsApplication) getApplication(); int subscriptionId = bundle.getInt("subscriptionId"); app.setSubscriptionId(subscriptionId); netStateText = (TextView) findViewById(R.id.netState); sendedText = (TextView) findViewById(R.id.sended); waitingText = (TextView) findViewById(R.id.waiting); lastFinishTimeText = (TextView) findViewById(R.id.lastFinishTime); if (checkMoveTaskToBack) { // moveTaskToBack(true); } //绑定Service Intent intent = new Intent(MainActivity.this, MyService.class); bindService(intent, this, Service.BIND_AUTO_CREATE); PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, MyService.class.getName()); wakeLock.acquire(); } catch (Exception ex) { ex.printStackTrace(); } } @Override public void onServiceConnected(ComponentName name, IBinder service) { MyService.Binder binder = (MyService.Binder) service; MyService myService = binder.getService(); myService.setCallback(new MyService.Callback() { @Override public void onDataChange(Message msg) { handler.sendMessage(msg); } }); } @Override public void onServiceDisconnected(ComponentName name) { } private Handler handler = new Handler() { public void handleMessage(Message msg) { // 更新UI switch (msg.what) { case 1: if (msg.getData() != null) { netStateText.setText(msg.getData().getString("netStateText").toString()); } else { netStateText.setText("数据错误!"); } break; case 2: if (msg.getData() != null) { sendedText.setText("已发送:" + msg.getData().getInt("sended")); waitingText.setText("待发送:" + msg.getData().getInt("waiting")); if (msg.getData().getString("finishTime") != null) { lastFinishTimeText.setText("最后发送时间:" + msg.getData().getString("finishTime")); } } break; } } }; @Override protected void onDestroy() { if (wakeLock != null) { wakeLock.release(); wakeLock = null; } // TODO 自动生成的方法存根 super.onDestroy(); unbindService(this); app.getEngine().close(); }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
MyService.java
public class MyService extends Service implements SMSResultListener { private Callback callback; SmsApplication app; @Nullable @Override public IBinder onBind(Intent intent) { return new Binder(); } public class Binder extends android.os.Binder { public MyService getService() { return MyService.this; } } private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String TAG ="MyService"; class UpdateNetState implements Runnable { @Override public void run() { android.os.Process .setThreadPriority(Process.THREAD_PRIORITY_URGENT_DISPLAY); while (true) { try { if (app.getEngine().connected()) { Message msg = new Message(); msg.what = 1; Bundle bundle = new Bundle(); bundle.putString("netStateText", "网络状态:正常"); Log.i(TAG,"网络状态:正常"+ System.currentTimeMillis()); msg.setData(bundle); if (callback != null) { callback.onDataChange(msg); } } else { Message msg = new Message(); msg.what = 1; Bundle bundle = new Bundle(); bundle.putString("netStateText", "网络状态:断开"); Log.i(TAG,"网络状态:断开"+ System.currentTimeMillis()); msg.setData(bundle); if (callback != null) { callback.onDataChange(msg); } } Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } } } } @Override public void onSended(int sended, int waiting, Date finishTime) { try { Message msg = new Message(); Bundle bundle = new Bundle(); bundle.putInt("sended", sended); bundle.putInt("waiting", waiting); msg.what = 2; if (finishTime != null) { bundle.putString("finishTime", sdf.format(finishTime)); } msg.setData(bundle); if (callback != null) { callback.onDataChange(msg); } // mActivityMessenger.send(msg); } catch (Exception ex) { ex.printStackTrace(); } } Thread updateNetState; @Override public void onCreate() { super.onCreate(); app = (SmsApplication) getApplication(); app.setSMSResultListener(this); updateNetState = new Thread(new UpdateNetState()); updateNetState.start(); } public void setCallback(Callback callback) { this.callback = callback; } public static interface Callback { void onDataChange(Message data); } @Override public void onDestroy() { super.onDestroy(); }}
SMSResultListener.java
public interface SMSResultListener { public void onSended(int sended, int waiting, Date finishTime);}
更多相关文章
- android 网络图片与网页读取
- Android(安卓)关闭所有的Activity
- Android(安卓)调用快递鸟api 实现物流跟踪(在途监控)
- android TextView 设置部分文本 边框和样式
- Android(安卓)Http网络开发神兵利器
- Intent在Android中的几种用法 .
- Android监听屏幕屏幕锁屏与解锁
- Android短信发送器
- android中去掉标题栏和状态栏