大家好我试图收到发送和接收短信计数。我在发送和接收短信时试图在数据库中保存计数。我在保存已发送的计数方面遇到问题。 Plz帮助我解决它。提前谢谢。
SentCount.java
public class SentCount extends Service {
public Context context;
private static int initialPos;
private static final String TAG = "SMSContentObserver";
private static final Uri uriSMS = Uri.parse("content://sms/sent");
public class content extends ContentObserver{
public content(Handler handler, Context ctx) {
super(handler);
context = ctx;
// TODO Auto-generated constructor stub
}
@Override
public void onChange(boolean selfChange){
//Context context = this.Context;
Log.d("onchage", "on cange calld");
DBConnect dbc = new DBConnect(context);
int duration=Toast.LENGTH_LONG;
ContentResolver cr = getContentResolver();
Cursor c = cr.query(Uri.parse("content://sms"), null, null, null, null);
if(c.moveToNext())
{
try{
int leastId = getLastid();
if(initialPos!=leastId)
{
String number = c.getString(c.getColumnIndex("Address"));
String date = c.getString(c.getColumnIndex("Date"));
String msg = c.getString(c.getColumnIndex("body"));
initialPos = leastId;
dbc.SaveSentCount(number, date);
Log.d("Sent to:", "SMS Sent to:"+number+" on:"+date+ " Msg:"+msg);
}
}
catch(Exception e){
Log.v("Tag", e.getMessage());
}
}
c.close();
}
private int getLastid() {
ContentResolver con = getContentResolver();
Cursor c = con.query(uriSMS, null, null, null, null);
c.moveToFirst();
int lastid = c.getInt(c.getColumnIndex("_id"));
Log.d("lastid:", "Saved"+lastid );
c.close();
return lastid;
}
@Override
public boolean deliverSelfNotifications() {
return false;
}
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
content c = new content(new Handler(), context);
ContentResolver cr = getBaseContext().getContentResolver();
cr.registerContentObserver(Uri.parse("content://sms"), true, c);
Toast.makeText(this, "The new Service was Created", Toast.LENGTH_LONG).show();
Log.d("","Service Startd");
}
@Override
public void onStart(Intent intent, int startId) {
// For time consuming an long tasks you can launch a new thread here...
// Toast.makeText(this, " Service Started", Toast.LENGTH_LONG).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return Service.START_STICKY;
}
@Override
public void onDestroy() {
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
}
DBConnect.java
public class DBConnect extends SQLiteOpenHelper{
private static final String SNUMB = "Number";
private static final String NUMB = "Number";
private static final String SDATE = "Date";
private static final String DATE = "Date";
private Context context;
public DBConnect(Context context) {
super(context, "SMSLogDB.db", null, 1);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS ReceiveSms(`Number` TEXT, `Date` TEXT)");
db.execSQL("CREATE TABLE IF NOT EXISTS SentSms(`Number` TEXT, `Date` TEXT)");
Log.d("","ReceiveSmsTable Created");
Log.d("","SentSmsTable Created");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void SaveReceivCount(String pnum, String date)
{
try{
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(NUMB, pnum);
values.put(DATE, date);
database.insert("ReceiveSms", null, values);
database.close();
Log.d("noti:", "Recevd sms insertd");
}
catch(Exception e)
{
Log.d("SaveReceivCount Exception:", ""+e);
}
}
public void SaveSentCount(String num, String dte)
{
try{
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(NUMB, num);
values.put(DATE, dte);
database.insert("SentSms", null, values);
database.close();
Log.d("noti:", "Sent sms insertd");
}
catch(Exception e)
{
Log.d("SaveSentCount Exception:", ""+e);
}
}
public int GetReceivCount()
{
Context context = null;
String cdate = DateFormat.getDateInstance().format(new Date());
SQLiteDatabase database = this.getReadableDatabase();
Cursor c = database.rawQuery("SELECT * FROM receiveSms WHERE Date = '"+cdate+"'", null);
Log.d("date_is",cdate);
int count = c.getCount();
c.close();
return count;
}
public int GetSentCount()
{
SQLiteDatabase database = this.getWritableDatabase();
Cursor c = database.rawQuery("SELECT * FROM SentSms",null);
int count = c.getCount();
c.close();
return count;
}
}
Presentcountactivity.java
public class PresentCountActivity extends Activity {
DBConnect db = new DBConnect(this);
TextView sent;
TextView received;
TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.presentcount);
t = (TextView)findViewById(R.id.textView1);
Intent in = getIntent();
String title = in.getStringExtra("c");
t.setText(title);
ReceivdCount();
SentCount();
}
public void ReceivdCount()
{
received = (TextView)findViewById(R.id.received);
String c = String.valueOf(db.GetReceivCount());
received.setText(c);
}
public void SentCount()
{
sent = (TextView)findViewById(R.id.sent);
String c = String.valueOf(db.GetSentCount());
sent.setText(c);
}
}
的Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sms.smslog"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.sms.activites.IndexActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.sms.activites.PresentCountActivity"></activity>
<activity android:name="com.sms.activites.OverallCountActivity"></activity>
<activity android:name="com.sms.activites.CountByContactActivity"></activity>
<activity android:name="com.sms.activites.SettingsActivity"></activity>
<receiver android:name="com.sms.smslog.RecievdCount">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
<service android:name = "com.sms.smslog.SentCount"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_SENT"></action>
</intent-filter>
</service>
</application>
<uses-permission android:name = "android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
</manifest>
Logcat是这样的:
02-28 21:43:06.149: D/onchage(5340): on cange calld
02-28 21:43:06.677: D/lastid:(5340): Saved5117
02-28 21:43:06.682: D/SaveSentCount Exception:(5340): java.lang.NullPointerException
02-28 21:43:06.682: D/Sent to:(5340): SMS Sent to:9743834990 on:1393603979999 Msg:hi
02-28 21:43:06.685: W/Trace(5340): Unexpected value from nativeGetEnabledTags: 0
02-28 21:43:06.687: W/Trace(5340): Unexpected value from nativeGetEnabledTags: 0