在Android的消息可点击的链接 [英] clickable link in message android

查看:129
本文介绍了在Android的消息可点击的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新到Android,我需要你的帮助。
我想要显示的警告对话框新传入的消息?并使可点击邮件中的内容链接

这是我的邮件活动

 公共类SecureMessagesActivity扩展活动实现OnClickListener,OnItemClickListener
 {
  公共无效的onCreate(捆绑savedInstanceState)
   {
      super.onCreate(savedInstanceState);    setTheme(android.R.style.Theme_Light);
     的setContentView(R.layout.main);    / **
     *您还可以在这里注册你的意图过滤器。
     *这里是例子,如何做到这一点。
     *
     * IntentFilter的过滤器=新的IntentFilter(android.provider.Telephony.SMS_RECEIVED);
     * filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
     * registerReceiver(新SmsReceiver(),过滤器);
    ** /    this.findViewById(R.id.UpdateList).setOnClickListener(本);
   }    ArrayList的<串GT; smsList =新的ArrayList<串GT;();  公共无效onItemClick(适配器视图<>母公司,观景,INT POS,长I​​D)
{
    尝试
    {
        的String [] =分裂smsList.get(POS).split(\\ n);
        串发件人=分裂[0];
        串的EncryptedData =;
        的for(int i = 1; I< splitted.length ++ I)
        {
            的EncryptedData + =分裂[I]
        }
        字符串数据=发件人+\\ n+ StringCryptor.decrypt(新的String(SmsReceiver.PASSWORD)的EncryptedData);
        Toast.makeText(这一点,数据,Toast.LENGTH_SHORT).show();
     }
    赶上(例外五)
    {
        e.printStackTrace();
    }
    }   公共无效的onClick(视图v)
  {    ContentResolver的ContentResolver的= getContentResolver();
    光标光标= contentResolver.query(Uri.parse(内容://短信/收件箱),NULL,NULL,NULL,NULL);    INT indexBody = cursor.getColumnIndex(SmsReceiver.BODY);
    INT indexAddr = cursor.getColumnIndex(SmsReceiver.ADDRESS);
    // INT indexCont = cursor.getColumnIndex(SmsReceiver.READ);    如果(indexBody<!0 || cursor.moveToFirst())回报;    建设者建设者=新AlertDialog.Builder(本);
     builder.setTitle(简单警报);
     builder.setMessage(这是简单的警告框);
     builder.setPositiveButton(OK,新DialogInterface.OnClickListener(){        @覆盖
        公共无效的onClick(DialogInterface对话,诠释它){
            // TODO自动生成方法存根
            dialog.dismiss();
        }     });
    smsList.clear();    做
    {
        字符串str =发件人:+ cursor.getString(indexAddr)+\\ n+ cursor.getString(indexBody);
        smsList.add(STR);
    }
    而(cursor.moveToNext());
    ListView控件smsListView =(ListView控件)findViewById(R.id.SMSList);
    smsListView.setAdapter(新ArrayAdapter<串GT;(这一点,android.R.layout.simple_list_item_1,smsList));
    smsListView.setOnItemClickListener(本);
 } 公共无效DisplayAlert(){    AlertDialog.Builder建设者=新AlertDialog.Builder(本);
     builder.setTitle(简单警报);
     builder.setMessage(R.string.app_name);
     builder.setPositiveButton(OK,新DialogInterface.OnClickListener(){        @覆盖
        公共无效的onClick(DialogInterface对话,诠释它){
            // TODO自动生成方法存根
            dialog.dismiss();        }
         });
     AlertDialog警报= builder.create();
     alert.show();
}

我smsReceive.java

 公共类SmsReceiver扩展广播接收器
    {
//在SMS表所有可用的列名
     // [_id,thread_id单,地址,
//人,日期,协议,阅读,
//状态,类型,reply_path_ present,
//主题,正文,service_center,
//锁定,错误_ code,可见]公共静态最后弦乐SMS_EXTRA_NAME =的PDU;
公共静态最后弦乐SMS_URI =内容://短信;公共静态最后弦乐ADDRESS =地址;
     公共静态最后弦乐PERSON =人;
   公共静态最后弦乐日期=日期;
   公共静态最后弦乐读=读;
   公共静态最后弦乐STATUS =身份;
   公共静态最后弦乐TYPE =类型;
   公共静态最后弦乐BODY =身体;
   公共静态最后弦乐SEEN =看见;   公共静态最终诠释MESSAGE_TYPE_INBOX = 1;
    公共静态最终诠释MESSAGE_TYPE_SENT = 2;    公共静态最终诠释MESSAGE_IS_NOT_READ = 0;
  公共静态最终诠释MESSAGE_IS_READ = 1;   公共静态最终诠释MESSAGE_IS_NOT_SEEN = 0;
   公共静态最终诠释MESSAGE_IS_SEEN = 1;  //此处更改密码或给用户的可能性,以改变它
   公共静态最后一个字节[] PASSWORD =新的字节[] {为0x20,0x32,0x34,0X47(字节)的0x84,0x33,将0x58}; 公共无效的onReceive(上下文的背景下,意图意图)
{
    //从意向短信地图
     捆绑额外= intent.getExtras();    串消息=;    如果(临时演员!= NULL)
    {
        //获取收到的短信阵列
        [对象] smsExtra =(对象[])extras.get(SMS_EXTRA_NAME);        //获取ContentResolver的对象推加密短信传入的文件夹
        ContentResolver的ContentResolver的= context.getContentResolver();        的for(int i = 0; I< smsExtra.length ++ I)
        {
            SmsMessage短信= SmsMessage.createFromPdu((字节[])smsExtra [I]);            绳体= sms.getMessageBody()的toString()。
            字符串的地址= sms.getOriginatingAddress();            消息+ = +地址+从短信:\\ n;
            消息+ =身体+\\ n;            //在这里你可以添加任何你code与接收到的SMS工作
            //我加加密所有收到的短信            putSmsToDatabase(ContentResolver的,短信);
        }
        //显示短信
      // Toast.makeText(背景下,消息Toast.LENGTH_SHORT).show();
      }
}私人无效putSmsToDatabase(ContentResolver的ContentResolver的,SmsMessage SMS)
{
    //创建短信排
    ContentValues​​值=新ContentValues​​();
    values​​.put(地址,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(可见,MESSAGE_IS_NOT_SEEN);
    尝试
    {
        字符串encryptedPassword = StringCryptor.encrypt(新的String(密码),sms.getMessageBody()的toString());
        values​​.put(BODY,encryptedPassword);
    }
    赶上(例外五)
    {
        e.printStackTrace();
    }    //推行插入SMS表
    contentResolver.insert(Uri.parse(SMS_URI),价值);
}
    }


解决方案

如果你试图打开短信收件箱,这应该有助于:

<一个href=\"http://stackoverflow.com/questions/1997328/android-clickable-hyperlinks-in-alertdialog\">Android:在AlertDialog 可点击超链接

,意图启动SMS应用

使用 Linkify 制作一个链接。

I'm new to android, I need your help. what I'm trying to display a new incoming message on alert dialog ?? and make Clickable the content "link" in the message

this my message activity

  public class SecureMessagesActivity extends Activity implements OnClickListener,    OnItemClickListener
 {
  public void onCreate(Bundle savedInstanceState) 
   {
      super.onCreate(savedInstanceState);

    setTheme( android.R.style.Theme_Light );
     setContentView(R.layout.main);

    /**
     * You can also register your intent filter here.
     * And here is example how to do this.
     *
     * IntentFilter filter = new IntentFilter(    "android.provider.Telephony.SMS_RECEIVED" );
     * filter.setPriority( IntentFilter.SYSTEM_HIGH_PRIORITY );
     * registerReceiver( new SmsReceiver(), filter );
    **/

    this.findViewById( R.id.UpdateList ).setOnClickListener( this );
   }

    ArrayList<String> smsList = new ArrayList<String>();

  public void onItemClick( AdapterView<?> parent, View view, int pos, long id ) 
{
    try 
    {
        String[] splitted = smsList.get( pos ).split("\n"); 
        String sender = splitted[0];
        String encryptedData = "";
        for ( int i = 1; i < splitted.length; ++i )
        {
            encryptedData += splitted[i];
        }
        String data = sender + "\n" + StringCryptor.decrypt( new String(SmsReceiver.PASSWORD), encryptedData );
        Toast.makeText( this, data, Toast.LENGTH_SHORT ).show();
     } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }
    }

   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 );
    //int indexCont = cursor.getColumnIndex(SmsReceiver.READ);

    if ( indexBody < 0 || !cursor.moveToFirst() ) return;

    Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("Simple Alert");  
     builder.setMessage("This is simple alert box");
     builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            dialog.dismiss();  
        }

     });
    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 );
 }

 public void DisplayAlert() {

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
     builder.setTitle("Simple Alert");  
     builder.setMessage(R.string.app_name);
     builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            dialog.dismiss();  

        }
         });
     AlertDialog alert = builder.create();  
     alert.show();          
}

my smsReceive.java

  public class SmsReceiver extends BroadcastReceiver 
    {
// All available column names in SMS table
     // [_id, thread_id, address, 
// person, date, protocol, read, 
// status, type, reply_path_present, 
// subject, body, service_center, 
// locked, error_code, seen]

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 );
}
    }

解决方案

If you're trying to open the SMS inbox, this should help:

Android: Clickable hyperlinks in AlertDialog

launch sms application with an intent

Use Linkify for making a link.

这篇关于在Android的消息可点击的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆