我有一个MainActivity,它显示一个带有CurrentLocation的Map和一个应该调用Help的按钮。
然后我有一个ContactView,我可以在TextView中加载联系人和消息。
我想要做的是:当我按下MainActivity中的帮助按钮时,我想拨打联系人和来自ContactView的消息并发送短信。
现在我不知道如何以及在何处实施onClick,因为在我的ContactView中,我已经使用onClick导入联系人。
这是我的MainActivity:
public class MainActivity extends AppCompatActivity {
private TextView message;
Button sendsms;
// GoogleMap class
private GoogleMap googleMap;
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater= getMenuInflater();
inflater.inflate(R.menu.contact_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if(id == R.id.action_contactView)
{
Intent ContactIntent = new Intent(this, ContactView.class);
startActivity(ContactIntent);
}
return true;
}
//zooms to currentLocation when the Position is obtained.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar();
message = (TextView) findViewById(R.id.message);
String msg=message.getText().toString();
sendsms = (Button)findViewById(R.id.helpButton);
googleMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
googleMap.setMyLocationEnabled(true);
GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange (Location location) {
LatLng loc = new LatLng (location.getLatitude(), location.getLongitude());
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16.0f));
}
};
googleMap.setOnMyLocationChangeListener(myLocationChangeListener);
// verify we can interact with the Google Map{
try {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().
findFragmentById(R.id.map)).getMap();
}
// Show a satellite map with roads
/* MAP_TYPE_NORMAL: Basic map with roads.
MAP_TYPE_SATELLITE: Satellite view with roads.
MAP_TYPE_TERRAIN: Terrain view without roads.
*/
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
// Enables indoor maps
googleMap.setIndoorEnabled(true);
// Show Zoom buttons
googleMap.getUiSettings().setZoomControlsEnabled(true);
} catch (Exception e) {
e.printStackTrace();
}
}
public void onClick(View v) {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNo,null,msg,null,null);
}
我已经开始在我的MainActivity中创建和点击以发送短信。但是ContactView中的phoneNo和msg无法在那里使用。
这是我的ContactView:
public class ContactView extends AppCompatActivity {
private static final int RESULT_PICK_CONTACT = 85;
private TextView textView1;
private TextView textView2;
private TextView message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_view);
textView1 = (TextView) findViewById(R.id.TxtName);
textView2 = (TextView) findViewById(R.id.TxtNumber);
message = (TextView) findViewById(R.id.message);
//speicher den aktuellen status der activity
SharedPreferences settings = getSharedPreferences("SelectedContact", MODE_PRIVATE);
String name = settings.getString("contactName", "");
//the second parameter set a default data if “contactName” is empty
if (!name.isEmpty()){
textView1.setText(name);
}
String phoneNo = settings.getString("contactPhone", "");//the second parameter set a default data if “contactName” is empty
if (!phoneNo.isEmpty()){
textView2.setText(phoneNo);
}
}
public void onClick(View v) {
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// check whether the result is ok
if (resultCode == RESULT_OK) {
// Check for the request code, we might be usign multiple startActivityForReslut
switch (requestCode) {
case RESULT_PICK_CONTACT:
contactPicked(data);
break;
}
} else {
Log.e("ContactView", "Failed to pick contact");
}
}
/**
* Query the Uri and read contact details. Handle the picked contact data.
*
* @param data
*/
private void contactPicked(Intent data) {
Cursor cursor = null;
try {
String phoneNo = null;
String name = null;
String msg=message.getText().toString();
//String ResqMessage = null;
// getData() method will have the Content Uri of the selected contact
Uri uri = data.getData();
//Query the content uri
cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
// column index of the phone number
int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
// column index of the contact name
int nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
phoneNo = cursor.getString(phoneIndex);
name = cursor.getString(nameIndex);
// Set the value to the textviews
textView1.setText(name);
textView2.setText(phoneNo);
SharedPreferences settings = getSharedPreferences("SelectedContact", MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString("contactName", name);
editor.putString("contactPhone", phoneNo);
editor.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
答案 0 :(得分:0)
在您的MainActivity中,您已经通过SMSManager在onClick methdo中发送短信。在此之下,您可以添加此链接中给出的代码来拨打电话。 How to make a phone call in android and come back to my activity when the call is done?
EndCallListener callListener = new EndCallListener();
TelephonyManager mTM = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);
private class EndCallListener extends PhoneStateListener {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if(TelephonyManager.CALL_STATE_RINGING == state) {
Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
}
if(TelephonyManager.CALL_STATE_OFFHOOK == state) {
//wait for phone to go offhook (probably set a boolean flag) so you know your app initiated the call.
Log.i(LOG_TAG, "OFFHOOK");
}
if(TelephonyManager.CALL_STATE_IDLE == state) {
//when this state occurs, and your flag is set, restart your app
Log.i(LOG_TAG, "IDLE");
}
}
}
在manifest.xml中添加以下权限。
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>