package com.example.gajah.mydemo; import android.app.Activity; import android.app.AlertDialog; import android.content.BroadcastReceiver; import android.content.ContentValues; import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.provider.Telephony; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import com.development.util.sms.SmsTools; import com.development.util.sms.smsUpdateData; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; public class MainActivity extends Activity implements smsUpdateData { private final String Tag = "MainActivity"; private ListView listView; private BroadcastReceiver myReceiver; private SmsTools tool = SmsTools.getInstance(this); private static MySmsAdapter adapter; private smsdata mdata; private List<smsdata> mlist; private int Pos, Tid; private static boolean bool = false ; final String myPackageName = "com.example.gajah.mydemo"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listView); adapter = new MySmsAdapter(MainActivity.this, R.layout.smsitem); listView.setAdapter(adapter); if (tool.sdkGreaterKitkat()) { // 判断SDK版本是不是大于等于4.4 Log.d(Tag, "sdkVersion = " + Integer.valueOf(android.os.Build.VERSION.SDK_INT)); if (!Telephony.Sms.getDefaultSmsPackage(this).equals(myPackageName)) { // 判断当前包名是否为默认包名 if (bool) { Log.d(Tag,"bool = "+(bool)); new onClickListenermlp3(); } else { showDialogWindows(bool); } } } tool.registerSmsReceiver(MainActivity.this, myReceiver); // setListdata(); updataListView(); listView.setOnItemClickListener(new getOnItemClickListenermlp()); } private void setListdata() { setSmsdata(); if (setSmsdata() != null) { tool.insert(setSmsdata()); } } /** * 测试时设置数据 * @return */ public ContentValues setSmsdata() { ContentValues values = new ContentValues(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss "); Date curDate = new Date(System.currentTimeMillis());//获取当前时间 String str = formatter.format(curDate); values.put("date", str); values.put("telphone", "400-40040-404"); values.put("body", "InkCase Now Receiver sms !"); values.put("status", 1); values.put("read", 0); values.put("ServiceCenterAddress", "2356798"); return values; } /** * 回调接口 更新UI界面 显示所有数据库未读短信 * @param */ public void updataListView() { Cursor cursor = tool.queryByRead(0); // 查询所有未读的短信 List<smsdata> list = new ArrayList<smsdata>(); while (cursor.moveToNext()) { list = tool.initListData(cursor, list); if (list == null) { return; } adapter.updateListDate(list); adapter.notifyDataSetChanged(); } cursor.close(); tool.closedb(MainActivity.this); } @Override protected void onDestroy() { super.onDestroy(); tool.closedb(MainActivity.this); tool.unregisterSmsReceiver(MainActivity.this, myReceiver); } /** * 显示对话框 * @param bool */ private void showDialogWindows(boolean bool) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.create(); if (bool) { builder.setTitle(R.string.dialog); builder.setMessage(R.string.message); builder.setNegativeButton(R.string.exit, new onClickListenermlp2()); builder.setPositiveButton(R.string.ok, new onClickListenermlp1()); } else { builder.setTitle(R.string.permission); builder.setMessage(R.string.change); builder.setNegativeButton(R.string.exit, new onClickListenermlp2()); builder.setPositiveButton(R.string.ok, new onClickListenermlp3()); } builder.show(); } /** * 删除适配器和数据库的数据并更新UI显示 */ private class onClickListenermlp1 implements DialogInterface.OnClickListener { @Override public void onClick(DialogInterface dialog, int which) { mlist = (List<smsdata>) adapter.getItem(Pos); mdata = mlist.get(Tid); shownSmsdata(mdata); mlist.remove(Tid); //删除适配器中被选中数据 adapter.setBoolean(true); // 更新未读信息数量显示 adapter.cleanData(); // 清除适配器数据 adapter.updateListDate(mlist); // 更新适配器数据 adapter.notifyDataSetChanged(); if (tool.setReadBySmsdata(mdata) == 0) // 删除数据库对应数据 { return; } } } private class onClickListenermlp2 implements DialogInterface.OnClickListener { @Override public void onClick(DialogInterface dialog, int which) { Log.d(Tag, " cancle Dialog "); dialog.cancel(); } } private class getOnItemClickListenermlp implements AdapterView.OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { showDialogWindows(true); Pos = position; Tid = (int) id; } } /** * 显示短信数据内容 * @param data */ private void shownSmsdata(smsdata data) { Log.d(Tag, "============== smsdata info ==> the start ===================="); Log.d(Tag, "smsdata Telphone = " + data.getTel()); Log.d(Tag, "smsdata data = " + data.getDate()); Log.d(Tag, "smsdata body = " + data.getBody()); Log.d(Tag, "smsdata read = " + data.getRead()); Log.d(Tag, "smsdata smsCounter = " + data.getSmsCounter()); Log.d(Tag, "============== smsdata info ==> the end ===================="); } /** * 将当前应用设置为默认接收短信服务 */ class onClickListenermlp3 implements DialogInterface.OnClickListener { @Override public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT); intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, myPackageName); Log.d(Tag, "intent = " + intent); startActivity(intent); bool = true; } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }