在android中发送短信,并将短信数据添加到短信电话数据库中

在android中发送短信,并将短信数据添加到短信电话数据库中,第1张

概述我尝试以编程方式向thistopic发送短信.短信发送正常,但是短信内容,实际上我的短信文本没有保存到电话信息数据库中当我在手机上打开SMS应用程序时,看不到已发送的消息.所以问题是:以编程方式发送短信后,有什么方法可以将短信保存在手机SMS数据库中?解决方法:我找到了一个很好的示

我尝试以编程方式向this topic发送短信.

短信发送正常,但是短信内容,实际上我的短信文本没有保存到电话信息数据库中
当我在手机上打开SMS应用程序时,看不到已发送的消息.

所以问题是:以编程方式发送短信后,有什么方法可以将短信保存在手机SMS数据库中?

解决方法:

我找到了一个很好的示例,您可以在其中阅读用户发送/接收的消息.

代码如下:

这是为了安全性的东西:

public class StringCryptor {    private static final String CIPHER_ALGORITHM = "AES";    private static final String RANDOM_GENERATOR_ALGORITHM = "SHA1PRNG";    private static final int RANDOM_KEY_SIZE = 128;    // Encrypts string and encode in Base64    public static String encrypt( String password, String data ) throws Exception     {        byte[] secretKey = generateKey( password.getBytes() );        byte[] clear = data.getBytes();        SecretKeySpec secretKeySpec = new SecretKeySpec( secretKey, CIPHER_ALGORITHM );        Cipher cipher = Cipher.getInstance( CIPHER_ALGORITHM );        cipher.init( Cipher.ENCRYPT_MODE, secretKeySpec );        byte[] encrypted = cipher.doFinal( clear );        String encryptedString = Base64.encodetoString( encrypted, Base64.DEFAulT );        return encryptedString;    }    // Decrypts string encoded in Base64    public static String decrypt( String password, String encryptedData ) throws Exception     {        byte[] secretKey = generateKey( password.getBytes() );        SecretKeySpec secretKeySpec = new SecretKeySpec( secretKey, CIPHER_ALGORITHM );        Cipher cipher = Cipher.getInstance( CIPHER_ALGORITHM );        cipher.init( Cipher.DECRYPT_MODE, secretKeySpec );        byte[] encrypted = Base64.decode( encryptedData, Base64.DEFAulT );        byte[] decrypted = cipher.doFinal( encrypted );        return new String( decrypted );    }    public static byte[] generateKey( byte[] seed ) throws Exception    {        KeyGenerator keyGenerator = KeyGenerator.getInstance( CIPHER_ALGORITHM );        SecureRandom secureRandom = SecureRandom.getInstance( RANDOM_GENERATOR_ALGORITHM );        secureRandom.setSeed( seed );        keyGenerator.init( RANDOM_KEY_SIZE, secureRandom );        SecretKey secretKey = keyGenerator.generateKey();        return secretKey.getEncoded();    }}

接着,

当您收到任何消息时,这里是如何处理的:

public class SmsReceiver extends broadcastReceiver {    public static final String SMS_EXTRA_name = "pdus";    public static final String SMS_URI = "content://sms";    public static final String ADDRESS = "address";    public static final String PERSON = "person";    public static final String DATE = "date";    public static final String READ = "read";    public static final String STATUS = "status";    public static final String TYPE = "type";    public static final String BODY = "body";    public static final String SEEN = "seen";    public static final int MESSAGE_TYPE_INBox = 1;    public static final int MESSAGE_TYPE_SENT = 2;    public static final int MESSAGE_IS_NOT_READ = 0;    public static final int MESSAGE_IS_READ = 1;    public static final int MESSAGE_IS_NOT_SEEN = 0;    public static final int MESSAGE_IS_SEEN = 1;    // Change the password here or give a user possibility to change it    public static final byte[] PASSWORD = new byte[]{ 0x20, 0x32, 0x34, 0x47, (byte) 0x84, 0x33, 0x58 };    public voID onReceive( Context context, Intent intent )     {        // Get SMS map from Intent        Bundle extras = intent.getExtras();        String messages = "";        if ( extras != null )        {            // Get received SMS array            Object[] smsExtra = (Object[]) extras.get( SMS_EXTRA_name );            // Get ContentResolver object for pushing encrypted SMS to incoming folder            ContentResolver contentResolver = context.getContentResolver();            for ( int i = 0; i < smsExtra.length; ++i )            {                SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);                String body = sms.getMessageBody().toString();                String address = sms.getoriginatingAddress();                messages += "SMS from " + address + " :\n";                                    messages += body + "\n";                // Here you can add any your code to work with incoming SMS                // I added encrypting of all received SMS                 putSmsToDatabase( contentResolver, sms );            }            // display SMS message            Toast.makeText( context, messages, Toast.LENGTH_SHORT ).show();        }    }

为了保存数据:

private voID putSmsToDatabase( ContentResolver contentResolver, SmsMessage sms )    {        // Create SMS row        ContentValues values = new ContentValues();        values.put( ADDRESS, sms.getoriginatingAddress() );        values.put( DATE, sms.getTimestampMillis() );        values.put( READ, MESSAGE_IS_NOT_READ );        values.put( STATUS, sms.getStatus() );        values.put( TYPE, MESSAGE_TYPE_INBox );        values.put( SEEN, MESSAGE_IS_NOT_SEEN );        try        {            String encryptedPassword = StringCryptor.encrypt( new String(PASSWORD), sms.getMessageBody().toString() );             values.put( BODY, encryptedPassword );        }        catch ( Exception e )         {             e.printstacktrace();         }        // Push row into the SMS table        contentResolver.insert( Uri.parse( SMS_URI ), values );    }

现在,要读取存储在数据库中的值,

public voID onClick( VIEw v ) {    ContentResolver contentResolver = getContentResolver();    Cursor cursor = contentResolver.query( Uri.parse( "content://sms/inBox" ), null, null, null, null);    int indexBody = cursor.getColumnIndex( SmsReceiver.BODY );    int indexAddr = cursor.getColumnIndex( SmsReceiver.ADDRESS );    if ( indexBody < 0 || !cursor.movetoFirst() ) return;    smsList.clear();    do    {        String str = "Sender: " + cursor.getString( indexAddr ) + "\n" + cursor.getString( indexBody );        smsList.add( str );    }    while( cursor.movetoNext() );    ListVIEw smsListVIEw = (ListVIEw) findVIEwByID( R.ID.SMSList );    smsListVIEw.setAdapter( new ArrayAdapter<String>( this, androID.R.layout.simple_List_item_1, smsList) );    smsListVIEw.setonItemClickListener( this );}

基本上,在这种情况下,然后单击刷新,列表视图将更新为最新值.

我希望这能回答这个问题.

总结

以上是内存溢出为你收集整理的在android中发送短信,并将短信数据添加到短信电话数据库中全部内容,希望文章能够帮你解决在android中发送短信,并将短信数据添加到短信电话数据库中所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1088702.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存