sms 短信/采信

来源:互联网 发布:无缝贴图软件中文版 编辑:程序博客网 时间:2024/05/22 12:48

1,采信

a) 获得所有采信列表

public class MMSList extends ListActivity implements OnItemClickListener
{
private Cursor cursor;


@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
cursor = getContentResolver().query(Uri.parse("content://mms"), null,
null, null, null);


SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, cursor, new String[]
{ "sub" }, new int[]
{ android.R.id.text1 })
{


@Override
public void setViewText(TextView v, String text)
{
try
{
text = new String(text.getBytes("ISO-8859-1"), "UTF-8");
}
catch (Exception e)
{


}
super.setViewText(v, text);
}
};
setListAdapter(simpleCursorAdapter);
getListView().setOnItemClickListener(this);


}


@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id)
{
cursor.moveToPosition(position);
String mid = cursor.getString(cursor.getColumnIndex("_id"));
Intent intent = new Intent(this, MMSBrowser.class);
intent.putExtra("mid", mid);
startActivity(intent);


}
}

b)采信详情

public class MMSBrowser extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);


ScrollView scrollView = (ScrollView) getLayoutInflater().inflate(
R.layout.main, null);
LinearLayout linearLayout = (LinearLayout) scrollView
.findViewById(R.id.linearlayout);
String mid = getIntent().getStringExtra("mid");
Cursor cursor = getContentResolver().query(
Uri.parse("content://mms/part"), null, "mid=?", new String[]
{ mid }, "_id asc");
while (cursor.moveToNext())
{


String type = cursor.getString(cursor.getColumnIndex("ct"));
String text = cursor.getString(cursor.getColumnIndex("text"));
if (text != null)
text = text.replaceAll("\\r", "");
byte[] data = null;


Uri partUri = Uri.parse("content://mms/part/"
+ cursor.getString(cursor.getColumnIndex("_id")));


ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = null;


try
{
is = getContentResolver().openInputStream(partUri);
byte[] buffer = new byte[8192];
int len = 0;
while ((len = is.read(buffer)) >= 0)
{
baos.write(buffer, 0, len);
}


}
catch (IOException e)
{
}
finally
{
if (is != null)
{
try
{
is.close();
data = baos.toByteArray();
}
catch (IOException e)
{


}
}
}
if (type.toLowerCase().contains("text"))
{
TextView textView = (TextView) getLayoutInflater().inflate(
R.layout.text, null);
textView.setText(text);
linearLayout.addView(textView);
}
else if (type.toLowerCase().contains("image"))
{
ImageView imageView = (ImageView) getLayoutInflater().inflate(
R.layout.image, null);
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,
data.length);
imageView.setImageBitmap(bitmap);
linearLayout.addView(imageView);
}
else if (type.toLowerCase().contains("video"))
{


String filename = "/sdcard/temp.3gp";
ImageView imageView = (ImageView) getLayoutInflater().inflate(
R.layout.image, null);
if (!new File(filename).exists())
{
try
{


FileOutputStream fos = new FileOutputStream(filename);
fos.write(data);
fos.close();
}
catch (Exception e)
{
}
}
Bitmap bitmap = getVideoThumbnail(filename);
imageView.setImageBitmap(bitmap);
linearLayout.addView(imageView);
}
}


setContentView(scrollView);
}


public Bitmap getVideoThumbnail(String filename)
{
Bitmap bitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inDither = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;


String whereClause = MediaStore.Video.Media.DATA + " = '" + filename
+ "'";


Cursor cursor = getContentResolver().query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, new String[]
{ MediaStore.Video.Media._ID }, whereClause, null, null);
boolean delete = false;
if (cursor == null || cursor.getCount() == 0)
{
ContentValues values = new ContentValues();
values.put(MediaStore.Video.Media.DATA, filename);
getContentResolver().insert(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
cursor = getContentResolver().query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, new String[]
{ MediaStore.Video.Media._ID }, whereClause, null, null);
if (cursor == null || cursor.getCount() == 0)
return null;
delete = true;
}
cursor.moveToFirst();


String videoId = cursor.getString(cursor
.getColumnIndex(MediaStore.Video.Media._ID));


if (videoId == null)
{
return null;
}
cursor.close();
long videoIdLong = Long.parseLong(videoId);
bitmap = MediaStore.Video.Thumbnails.getThumbnail(getContentResolver(),
videoIdLong, Images.Thumbnails.MICRO_KIND, options);
if (delete)
{
getContentResolver().delete(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
MediaStore.Video.Media.DATA + "=?", new String[]
{ filename });
}
return bitmap;
}
}

2,发送短信并保存记录

public class SendSMS extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}


public void onClick_SendSMS(View view)
{

//发送短信

a)直接发送
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("10086", null, "this is test sms?", null, null);
Toast.makeText(this, "send text already.", Toast.LENGTH_LONG).show();
b)调用系统界面发送
Intent sendIntent = new Intent(Intent.ACTION_SENDTO, Uri.parse("sms:12345678"));
    sendIntent.putExtra("sms_body", "sms body");
    startActivity(sendIntent); 


//保存记录
Uri uri = Uri.parse("content://mms-sms/canonical-addresses");


Cursor cursor = getContentResolver().query(uri, null, "address=?",
new String[]
{ "10086" }, null);

String threadID = "";


if (cursor.moveToNext())
{
threadID = cursor.getString(0);
}
else  
{
return;
}

ContentValues contentValues = new ContentValues();
contentValues.put("thread_id", threadID);
contentValues.put("body", "sms body");
contentValues.put("date", new Date().getTime());
contentValues.put("address", 3544545);
contentValues.put("type", 2);
contentValues.put("read", 1);
getContentResolver().insert(Uri.parse("content://sms"), contentValues);
}
}

3,发送彩信

Intent intent = new Intent(Intent.ACTION_SEND);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("address", "10086");
intent.putExtra("compose_mode", false);
intent.putExtra("exit_on_sent", true);
intent.putExtra("subject", "sms subject");
intent.putExtra("sms_body", "sms body ");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/a.jpg"));
intent.setClassName("com.android.mms",
"com.android.mms.ui.ComposeMessageActivity");
intent.setType("image/jpeg");
startActivity(Intent.createChooser(intent, "Send MMS To"));

4,对短信进行监听

public class SMSReceiver extends BroadcastReceiver

{


@Override
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();

if (bundle != null)
{
Object[] objArray = (Object[]) bundle.get("pdus");


SmsMessage[] messages = new SmsMessage[objArray.length]; 

String body = "";
for (int i = 0; i < objArray.length; i++)
{
messages[i] = SmsMessage.createFromPdu((byte[]) objArray[i]);

body += messages[i].getDisplayMessageBody();
}
String phoneNumber = messages[0].getDisplayOriginatingAddress();
Toast.makeText(context, "" + phoneNumber + "\n" + body,
Toast.LENGTH_LONG).show();
}
}
}

<receiver android:name=".SMSReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>


0 0
原创粉丝点击