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);}

更多相关文章

  1. Android Http网络开发神兵利器
  2. android 网络图片与网页读取
  3. 判断Android客户端网络是否连接
  4. Android检测电源状态
  5. Android中网络判断
  6. Android沉浸式通知栏的一个开源库SystemBarTint简单使用,android
  7. 直播源码用Glide框架缓存压缩图片做一个网络图片查看器
  8. android 开源自组织网络开源包

随机推荐

  1. Android 3.0 r1中文API文档(103) —— In
  2. Android(java)学习笔记68:同步代码块的锁和
  3. Android(安卓)UI设计--ListView内嵌Check
  4. android ble蓝牙开发略解-Android 蓝牙4.
  5. 基于源码分析 Android View 绘制机制
  6. Android的启动模式(android:launchMode)
  7. Android - Android Studio修改字体(font)
  8. 去掉android程序的任务栏
  9. Android(安卓)OTA在线升级二(升级包编译原
  10. mac 下真机调试 android 手机