源码简介
根据选项选中运营商查询流量信息,调用系统发短信,系统查收到短信,自动将流量信息返回到页面上。
源码截图
源码片段:
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_service);
- initView();
- initContentObserver();
- }
- private void initView() {
- yongde = (TextView) findViewById(R.id.yong);
- shengxia = (TextView) findViewById(R.id.wu);
- }
- private void initContentObserver() {
- mUri = Uri.parse("content://sms/");
- mContentResolver = this.getContentResolver();
- mContentResolver.registerContentObserver(mUri, true,
- new SMSContentObserver(new Handler()));
- }
- private class SMSContentObserver extends ContentObserver {
- public SMSContentObserver(Handler handler) {
- super(handler);
- }
- public void onChange(boolean selfChange) {
- super.onChange(selfChange);
- String[] projection = new String[] { "_id", "address", "body",
- "type", "read" };
- String where = " address = '10086' AND read = '0'";
- Cursor cursor = mContentResolver.query(mUri, projection, where,
- null, "date desc");
- while (cursor.moveToNext()) {
- String address = cursor.getString(cursor
- .getColumnIndex("address"));
- String body = cursor.getString(cursor.getColumnIndex("body"));
- int id = cursor.getInt(cursor.getColumnIndex("_id"));
- String type = cursor.getString(cursor.getColumnIndex("type"));
- System.out.println(body);
- if (body.length() >= 10) {
- int yStart = body.indexOf("MB");
- int yEnd = body.indexOf("已使用");
- int wStart = body.lastIndexOf("MB");
- int wEnd = body.lastIndexOf("剩 余");
- String used = body.substring(yEnd,yStart);
- String Surplus = body.substring( wEnd,wStart);
- yongde.setText(used + " MB");
- shengxia.setText(Surplus + " MB");
- System.out.println(used + Surplus);
- }
- }
- }
- }
源码下载:http://down.51cto.com/data/2015697