控制后退按钮功能

时间:2013-12-18 19:15:36

标签: android sms hardware

从图片中的给定ListView,我选择发送短信的联系人并转移到另一个活动(第二张图片)。并从这个短信Activity图片。当我按下手机上的硬件按钮时,我应该移动到我想要的位置。我不希望显示默认Activity。我怎么能这样做?

public class SendSms extends Activity{
ListView listView;
List<RowItems> rowItems;
CustomListViewAdapter adapter;

String toNumbers = "";
String separator;
int i=0,j,count=0,count1; String a[]=new String[5];
String number,val2;

 @Override
  protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new4); 
listView = (ListView) findViewById(R.id.list);

rowItems = new ArrayList<RowItems>();

val2=getIntent().getStringExtra("name");
 ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null,   null);


 if (cur.getCount() > 0) {

    while (cur.moveToNext()) {
  String rawContactId =              cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));

  Cursor c = getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            new String[] {
                    ContactsContract.CommonDataKinds.Phone.NUMBER,
                   // ContactsContract.CommonDataKinds.Phone.TYPE,
                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                    ContactsContract.CommonDataKinds.Phone.PHOTO_ID
            }, ContactsContract.Data.RAW_CONTACT_ID + "=?", new String[] { rawContactId }, null);
  if (c != null) {
        if (c.moveToFirst()) {
            String number = c.getString(0);
            //int type = c.getInt(1);
            String name = c.getString(1);
            int photoId = c.getInt(2);
            Bitmap bitmap = queryContactImage(photoId);
            RowItems p=new RowItems(bitmap, number, name);
            rowItems.add(p);

        } 
        adapter = new CustomListViewAdapter(this,
                R.layout.new5, rowItems);
        listView.setAdapter(adapter);


        c.close();
    }
}
    }
}


 private Bitmap queryContactImage(int imageDataRow) {
Cursor c = getContentResolver().query(ContactsContract.Data.CONTENT_URI, new String[] {
    ContactsContract.CommonDataKinds.Photo.PHOTO
}, ContactsContract.Data._ID + "=?", new String[] {
    Integer.toString(imageDataRow)
}, null);
byte[] imageBytes = null;
if (c != null) {
    if (c.moveToFirst()) {
        imageBytes = c.getBlob(0);
    }
    c.close();
}

if (imageBytes != null) {
    return BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length); 
} else {
    return null;
  }
  } 
    public void showResult(View v) {
// String result = "";
String result1="";
    for (RowItems p : adapter.getBox()) {
      if (p.chck){
       // result += "\n" + p.getTitle();
        result1 +="\n" +p.getDesc();
        a[i]=p.getTitle();
        i++;
       count++;
      }
      count1=count; 
    } 

    Toast.makeText(this, result1+"\n", Toast.LENGTH_LONG).show();
  }



       public void send(View v) throws IOException {
    int value=a.length-count1;
    for(j=0;j<a.length-value;j++){   
         if(android.os.Build.MANUFACTURER.equalsIgnoreCase("Samsung")){   
                separator = ",";   
            } else   
                separator = ";";       


     number = a[j];   
    toNumbers= toNumbers +number +separator;   

}  

 toNumbers = toNumbers.substring(0, toNumbers.length());


    Uri sendSmsTo = Uri.parse("smsto:" + toNumbers);

    String smsValue = "help iam in danger..i am presently at \n"+val2;
    if(val2==null){
        Toast.makeText(SendSms.this, "wait.....", Toast.LENGTH_LONG).show();
    }

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setType("vnd.android-dir/mms-sms");
    intent.putExtra("sms_body", smsValue);
    intent.setData(sendSmsTo);

    startActivity(intent);

    }

4 个答案:

答案 0 :(得分:1)

使用startAtctivityForResult,并在onActivityResult中检查resultCode。如果是Activity.RESULT_CANCELLED,则用户'退出'SMS活动。由于SMS活动的实施不在您的控制范围之内,因此结果代码表示取消时可能不是唯一的情况。

答案 1 :(得分:0)

将此方法添加到类中以处理设备后退按钮。

@Override
public void onBackPressed() {
// do something
}

请注意,它需要API级别5或更高级别。

答案 2 :(得分:0)

我认为您应该在显示Activity图片时尝试调用onBackPressed方法。

// back button pressed method
@Override
public void onBackPressed() {
     super.onBackPressed();
     // new intent to call an activity that you choose
     Intent intent = new Intent(this, NewActivity.class);
     startActivity(intent);
     // finish the activity picture
     this.finish();
}

如果您不想拨打外部Activity,可以隐藏Intent拨打的布局,如下所示:

// back button pressed method
@Override
public void onBackPressed() {
     super.onBackPressed();
     // your layout with the picture is displayed, hide it
     if(MySecondView.getVisibility() == View.VISIBLE)
            MySecondView.setVisibility(View.Gone);  
     // display the layout that you want
     MyDefaultView.setVisibility(View.VISIBLE); 
}  

正如我在下面所说,尝试使用布尔,在Class开头初始化为false。当你启动Intent.ACTION_VIEW时,将此布尔值设为true:

public class Foo extends Activity {
    private boolean isSms = false;
    // some stuff  

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setType("vnd.android-dir/mms-sms");
    // ...
    startActivity(intent);  
    // make your boolean to true  
    isSms = true;  

    // create the onBackPressed method  
    @Override
    public void onBackPressed() {
          // check if the user is on the sms layout
          if(isSms)
              // do something  
          else
              // do something else like finish(); your activity

     }

 }  

或者您可以尝试使用onKeyDown方法(请参阅此处:onKeyDown (int keyCode, KeyEvent event)),如下所示:

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(keyCode==KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0) {
        // dome some stuff
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

您可以在此处看到这两种方法之间的差异:onBackPressed() not working以及我在下面的评论中所说的网站。
希望这会有用。


<强>更新
更好的方法是使startActivityForResult启动Intent.ACTION_VIEW:

startActivityForResult(intent, 1);  

然后,您将返回取消代码,如下所示:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode != 1 && resultCode == RESULT_CANCELED) {
        // do something
    }
}  

这是@FunkTheMonk,就在上面,他说出了不错的选择!

答案 3 :(得分:0)

您必须通过调用方法名称onbackpressed()来覆盖硬件后退按钮的返回行为;

@Override
public void onBackPressed() {
 super.onBackPressed();
 // set intent to activity where you want to move on after back press
 Intent intent = new Intent(this, NewActivity.class);
 startActivity(intent);
 // also clean the current activity from stack.
 youractivityname.this.finish();
}