此代码向我发送消息。并且从电话联系人检索联系人的联系人按钮不能正常工作。只能读取联系人,但选择时不想显示在EditText
上。有一条错误消息
Unfortunately.SMS Encryption has stopped
我不知道伤害在哪里。
form_create.java
public class form_create extends Activity {
protected static final int RQS_PICK_CONTACT = 1;
private EditText NoTujuan;
private String id, name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.form_create);
final Button enkripButton = (Button)findViewById(R.id.btnEncrypt);
final EditText inputKunci = (EditText)findViewById(R.id.txtKey);
final EditText inputPesan = (EditText)findViewById(R.id.txtMessage);
final EditText teksEnkrip = (EditText)findViewById(R.id.txtMesEncrypt);
final Button sendButton = (Button)findViewById(R.id.btnSend);
final Button mAddContact = (Button)findViewById(R.id.btnContact);
NoTujuan =(EditText)findViewById(R.id.txtPhone);
//button enkripsi
enkripButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String ambilKunci = inputKunci.getText().toString();
String ambilPesan = inputPesan.getText().toString();
String enPesan = "";
if(ambilKunci.length()==0){
Toast.makeText(form_create.this, "Key must be filled", Toast.LENGTH_SHORT).show();
}else if
(ambilKunci.length()>0 && ambilPesan.length()>0){
try {
enPesan = GenerateAES.encrypt(ambilKunci, ambilPesan);
}
catch (Exception e) {}
teksEnkrip.setText(enPesan);
}
else{
Toast.makeText(form_create.this, "Text can not be empty", Toast.LENGTH_LONG).show();
}
}
}
);
//button kirim
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String no = NoTujuan.getText().toString().trim();
String pesanEnkrip = teksEnkrip.getText().toString();
try{
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(no, null, pesanEnkrip, null, null);
Toast.makeText(getApplicationContext(), "SMS SENT",
Toast.LENGTH_LONG).show();
}catch (Exception e){
Toast.makeText(getApplicationContext(), "SMS FAILED", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
private void Kirim_Pesan(String phoneNo, String message) {
String SENT ="SMS_SENT";
String DELIVERED="SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(getBaseContext(), 0, new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(getBaseContext(), 0, new Intent(DELIVERED), 0);
//ketika sms akan dikirim
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS SENT", Toast.LENGTH_LONG).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS FAILED", Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No Signal", Toast.LENGTH_LONG).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), " Null PDU", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off", Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS SENT", Toast.LENGTH_LONG).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS FAILED", Toast.LENGTH_LONG).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
//sms.sendMultipartTextMessage(NoTujuan, null, teksEnkrip, sentPI, deliveredPI);
ArrayList<String> parts = sms.divideMessage(message);
sms.sendMultipartTextMessage(phoneNo, null, parts, null, null);
}});
//Button Contact
mAddContact.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent i = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(i, 1);
}
});
}
//proses pengambilan kontak
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
getContactData(data);
}
}
public void getContactData(Intent data)
{
ContentResolver cr = getContentResolver();
Uri contactData = data.getData();
Log.v("Contact", contactData.toString());
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst())
{
//ambil id contact
id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
Log.v("Contact", "ID: " + id.toString());
name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.v("Contact", "Name: " + name.toString());
//cek jumlah nomor pada contact yg dipilih
if (Integer.parseInt(c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
Cursor pCur = cr.query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + " = ?", new String[] {id}, null);
while (pCur.moveToNext())
{
//ambil nomor berdasarkan id yang dipilih
String phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String type = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
String s = (String) ContactsContract.CommonDataKinds.Phone.getTypeLabel(getResources(), Integer.parseInt(type), "");
Log.d("TAG", s + " phone: " + phone);
Log.d("TAG", s + " type: " + type);
Log.d("TAG", s + " s: " + s);
// tampilkan nomor
NoTujuan.setText(" " +phone);
}
pCur.close();
} }
}}