0

我正在开发一个 android 应用程序,它将接收来自特定号码的彩信并将其显示在我的应用程序中,我找到了这段代码,但是当我运行它时,什么也没发生

 public class MMSActivity extends Activity {
ImageView imageView1;
TextView t; 
MMSMonitor myMonitor = null;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    t= (TextView) findViewById(R.id.t);
    imageView1= (ImageView) findViewById(R.id.imageView1);
    startMMSMonitor();

}

@Override
public void onDestroy()
{
    super.onDestroy();
    if(myMonitor != null)
        myMonitor.stopMMSMonitoring();
}

protected void startMMSMonitor()
{
    Context ctx = this;
    ContentResolver cr = this.getContentResolver();
    myMonitor = new MMSMonitor(cr, ctx);
    myMonitor.startMMSMonitoring();
}

public void setMMSText(String text)
{
    //Do whatever you want
}

public void setMMSImageData(byte[] data, String fileType)
{
    //Do whatever you want
}

public class MMSMonitor { 

    private ContentResolver contentResolver = null;
    private Handler mmshandler = null;
    private ContentObserver mmsObserver = null;
    public String mmsNumber = "";
    public boolean monitorStatus = false;
    public  String activationCode;
    int mmsCount = 0;
    String lastMMSTxId = null;
    String code;

    public MMSMonitor(final ContentResolver contentResolver, final Context mainContext) {
        this.contentResolver = contentResolver;
        mmshandler = new MMSHandler();
        mmsObserver = new MMSObserver(mmshandler);
        System.out.println( "MMSMonitor :: ***** Start MMS Monitor *****");
    }

    public void startMMSMonitoring() {
        try {
            monitorStatus = false;
            if (!monitorStatus) {//do not get it
                //it is observe anychange like delete or incoming MMS etc...

                //ContentObserver is used to get notified if the data residing in the data set has changed. 
                        //So it is used to observe the data source for changes.
                //Content providers manage access to a structured set of data
                //Content providers are the standard interface that connects data 
                //in one process with code running in another process.
                //When you want to access data in a content provider, you use the ContentResolver object 
                contentResolver.registerContentObserver(Uri.parse("content://mms-sms"), true, mmsObserver);

                Uri uriMMSURI = Uri.parse("content://mms");
                Cursor mmsCur = contentResolver.query(uriMMSURI, null, "msg_box = 4", null, "_id");
                if (mmsCur != null && mmsCur.getCount() > 0) {
                    //Number of MMS
                    mmsCount = mmsCur.getCount();
                    System.out.println( "MMSMonitor :: Init MMSCount ==" + mmsCount);
                }
            }
        } catch (Exception e) {
            System.out.println( "MMSMonitor :: startMMSMonitoring Exception== "+ e.getMessage());
        }
    }


    public void stopMMSMonitoring() {
        try {
            monitorStatus = false;
            if (!monitorStatus){
                contentResolver.unregisterContentObserver(mmsObserver);
            }
        } catch (Exception e) {
            System.out.println( "MMSMonitor :: stopMMSMonitoring Exception == "+ e.getMessage());
        }
    }
      //A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. 

    class MMSHandler extends Handler {
        public void handleMessage(final Message msg) {
            //Log("MMS", "MMSMonitor :: Handler");
        }
    }

    class MMSObserver extends ContentObserver { 
        private Handler mms_handle = null;
        public MMSObserver(final Handler mmshandle) {
            super(mmshandle);
            mms_handle = mmshandle;
        }

        public void onChange(final boolean bSelfChange) { 
            super.onChange(bSelfChange);
            //Log("MMS", "MMSMonitor :: Onchange");

            Thread thread = new Thread() { 
                public void run() {
                    try { 
                        monitorStatus = true;

                        // Send message to Activity
                        Message msg = new Message();
                        mms_handle.sendMessage(msg);

                        // Getting the mms count
                        Uri uriMMSURI = Uri.parse("content://mms/");
                        Cursor mmsCur = contentResolver.query(uriMMSURI, null, "msg_box = 4 or msg_box = 1", null,"_id");

                        int currMMSCount = 0;
                        if (mmsCur != null && mmsCur.getCount() > 0) {
                            currMMSCount = mmsCur.getCount();
                        }

                        if (currMMSCount > mmsCount) {
                            mmsCount = currMMSCount;
                            mmsCur.moveToLast();

                            // get id , subject
                            //String subject = mmsCur.getString(6);
                            //int id = Integer.parseInt(mmsCur.getString(0));
                            String subject = mmsCur.getString(mmsCur.getColumnIndex("sub"));
                            int id = Integer.parseInt(mmsCur.getString(mmsCur.getColumnIndex("_id")));
                            System.out.println( "MMSMonitor :: _id  == " + id);
                            System.out.println( "MMSMonitor :: Subject == " + subject);

                            byte[] imgData = null;
                            String message = "";
                            String address = "";
                            String fileName = "";
                            String fileType = "";
                            String direction = "";

                            // GET DIRECTION
                            boolean isIncoming = false;
                            //int type = Integer.parseInt(mmsCur.getString(12));
                            int type = Integer.parseInt(mmsCur.getString(mmsCur.getColumnIndex("m_type")));
                            if (type == 128) {
                                direction = "0";
                                System.out.println( "MMSMonitor :: Type == Outgoing MMS");
                            } else {
                                isIncoming = true;
                                direction = "1";
                                System.out.println( "MMSMonitor :: Type == Incoming MMS");
                            }

                            // Get Parts
                            Uri uriMMSPart = Uri.parse("content://mms/part");
                            Cursor curPart = contentResolver
                            .query(uriMMSPart, null, "mid = " + id,   null, "_id");         
                            System.out.println( "MMSMonitor :: parts records length == "+ curPart.getCount());
                            curPart.moveToLast();
                            do {
                                //String contentType = curPart.getString(3);
                                //String partId = curPart.getString(0);
                                String contentType = curPart.getString(curPart.getColumnIndex("ct"));
                                String partId = curPart.getString(curPart.getColumnIndex("_id"));
                                System.out.println( "MMSMonitor :: partId == " + partId);
                                System.out.println( "MMSMonitor :: part mime type == "+ contentType);

                                // Get the message
                                if (contentType.equalsIgnoreCase("text/plain"))
                                {
                                    System.out.println("MMSMonitor :: ==== Get the message start ====");
                                    byte[] messageData = readMMSPart(partId);
                                    if (messageData != null && messageData.length > 0)
                                        message = new String(messageData);

                                    if(message == ""){
                                        Cursor curPart1 = contentResolver
                                        .query(uriMMSPart, null, "mid = " + id +
                                                " and _id =" + partId,null, "_id");
                                        for (int i = 0; i < curPart1.getColumnCount(); i++) 
                                        {
                                            System.out.println("MMSMonitor :: Column Name : " +
                                                    curPart1.getColumnName(i));
                                        }
                                        curPart1.moveToLast();
                                        message = curPart1.getString(13);
                                    }
                                    System.out.println("MMSMonitor :: Txt Message == " + message);

                                    //SEND DATA TO ACTIVITY
                                    setMMSText(message);
                                } 


                                // Get Image
                                else if (isImageType(contentType) == true) {

                                    System.out.println("MMSMonitor :: ==== Get the Image start ====");      
                                    fileName = "mms_" + partId;
                                    fileType = contentType;
                                    imgData = readMMSPart(partId);
                                    System.out.println( "MMSMonitor :: Iimage data length == "+ imgData.length);

                                    //SEND DATA TO ACTIVITY
                                    setMMSImageData(imgData, fileType);
                                }   
                            } while (curPart.moveToPrevious());


                        }

                    } catch (Exception e) {
                        System.out.println( "MMSMonitor Exception:: "+ e.getMessage());
                    }
                }
            }; 
            thread.start();
        } 
    } 


    private byte[] readMMSPart(String partId) {
        byte[] partData = null;
        Uri partURI = Uri.parse("content://mms/part/" + partId);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        InputStream is = null;

        try {

            System.out.println("MMSMonitor :: Entered into readMMSPart try..");
            ContentResolver mContentResolver = contentResolver;
            is = mContentResolver.openInputStream(partURI);

            byte[] buffer = new byte[256];
            int len = is.read(buffer);
            while (len >= 0) {
                baos.write(buffer, 0, len);
                len = is.read(buffer);
            }
            partData = baos.toByteArray();
            //Log.i("", "Text Msg  :: " + new String(partData));

        } catch (IOException e) {
            System.out.println( "MMSMonitor :: Exception == Failed to load part data");
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    System.out.println("Exception :: Failed to close stream");
                }
            }
        }
        return partData;
    }


    private boolean isImageType(String mime) {
        boolean result = false;
        if (mime.equalsIgnoreCase("image/jpg")
                || mime.equalsIgnoreCase("image/jpeg")
                || mime.equalsIgnoreCase("image/png")
                || mime.equalsIgnoreCase("image/gif")
                || mime.equalsIgnoreCase("image/bmp")) {
            result = true;
        }
        return result;
    }

}

   }



//=====================


      }

任何答案我都会感激的,

还有这个声明应该做什么?

Cursor mmsCur = contentResolver.query(uriMMSURI, null, "msg_box = 4 or msg_box = 1", null,"_id");

4

1 回答 1

1

我认为你的方法甚至不是正确的。Afaik 你必须注册一个广播接收器并在你的应用程序中处理/实现它才能实现你想要的。

Android MMS Broadcast receiver Detecting new MMS (Android 2.1) Detecting MMS messages on Android

有你需要的所有信息;)

于 2012-04-26T08:29:29.343 回答