使用contentobserver&获取SMS详细信息服务

时间:2013-06-19 11:43:47

标签: android sms contentobserver

我正在尝试通过以下代码检索已发送的SMS详细信息。但是,当我运行此代码时,我有一个空日志。任何人都可以识别错误。感谢

MainActivity.java:

package com.smsobserver;

import android.os.Bundle;

 import android.app.ListActivity;
 import android.content.Intent; 
 import android.view.Menu;


 public class MainActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    Intent sintent = new Intent(MainActivity.this,SMSservice.class);
    MainActivity.this.startService(sintent);
}


private boolean SSMyservice() {
    // TODO Auto-generated method stub


    //stop service      
    Intent sintent = new Intent(MainActivity.this,SMSservice.class);
    MainActivity.this.stopService(sintent);

    //do some work

    //start service again
    MainActivity.this.startService(sintent); //start service again


    return true;



} //importdata







@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

}

SMSservice.java:

package com.smsobserver;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

public class SMSservice extends Service {


protected SmsObserver smsSentObserver=null;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }


      @Override          
        public void onCreate() 
        {

            registersmsevent();

        }

      @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            // TODO Auto-generated method stub
            super.onStartCommand(intent, flags, startId);

             return Service.START_STICKY;  

        }



        @Override
        public void onDestroy() {
            // TODO Auto-generated method stub
            super.onDestroy();
            unregistersmsevent();
        }


    private void unregistersmsevent() {
            // TODO Auto-generated method stub
         if(smsSentObserver != null)
            {
                SMSservice.this.getContentResolver().unregisterContentObserver(smsSentObserver); 
                smsSentObserver = null;
            }

        }


    private void registersmsevent() {
        // TODO Auto-generated method stub
          if(smsSentObserver == null)
            {
            final Uri SMS_STATUS_URI = Uri.parse("content://sms");  
            smsSentObserver = new SmsObserver(new Handler());
            SMSservice.this.getContentResolver().registerContentObserver(SMS_STATUS_URI, true, smsSentObserver); 
            }
    }  


    public static class SmsObserver extends ContentObserver {


        private static final String TAG = "SMSTRACKER";
        private static final Uri STATUS_URI = Uri.parse("content://sms");

        private Context mContext;

        Handler handler; 

          public SmsObserver(Handler handler) {        
              super(handler);
              // TODO Auto-generated constructor stub                
              this.handler = handler; 
          }



          @Override 
        public boolean deliverSelfNotifications() {
            return true;
        }

          @Override 
        public void onChange(boolean selfChange) {

              //my code........

              try{
                Log.d(TAG, "Notification on SMS observer");
                Cursor sms_sent_cursor = mContext.getContentResolver().query(STATUS_URI, null, null, null, null);
                if (sms_sent_cursor != null) {
                    if (sms_sent_cursor.moveToFirst()) {
                        String protocol = sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("protocol"));
                        Log.d(TAG, "protocol : " + protocol);
                        if(protocol == null){
                            String[] colNames = sms_sent_cursor.getColumnNames();
                            int type = sms_sent_cursor.getInt(sms_sent_cursor.getColumnIndex("type"));
                            Log.e(TAG, "SMS Type : " + type);
                            if(type == 2){
                                Log.d(TAG, "Id : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("_id")));
                                Log.d(TAG, "Thread Id : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("thread_id")));
                                Log.d(TAG, "Address : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("address")));
                                Log.d(TAG, "Person : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("person")));
                                Log.d(TAG, "Date : " + sms_sent_cursor.getLong(sms_sent_cursor.getColumnIndex("date")));
                                Log.d(TAG, "Read : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("read")));
                                Log.d(TAG, "Status : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("status")));
                                Log.d(TAG, "Type : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("type")));
                                Log.d(TAG, "Rep Path Present : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("reply_path_present")));
                                Log.d(TAG, "Subject : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("subject")));
                                Log.d(TAG, "Body : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("body")));
                                Log.d(TAG, "Err Code : " + sms_sent_cursor.getString(sms_sent_cursor.getColumnIndex("error_code")));

                                if(colNames != null){
                                    for(int k=0; k<colNames.length; k++){
                                        Log.d(TAG, "colNames["+k+"] : " + colNames[k]);
                                    }
                                }

                            }
                        }
                    }
                }
                else
                    Log.e(TAG, "Send Cursor is Empty");
            }
            catch(Exception sggh){
                Log.e(TAG, "Error on onChange : "+sggh.toString());
            }

            super.onChange(selfChange);
        }



    }//End of class SmsObserver

}

我该怎样做才能在logcat或listview中显示细节?

0 个答案:

没有答案