如何以编程方式按下保存联系人按钮
How to press Save Contact Button Programmatically
我有代码可以将我带到保存联系人屏幕,填写我需要的详细信息,但是我现在需要它来自动按下保存联系人。有谁知道怎么做?或者如果有可能的话。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent contactIntent = new Intent(ContactsContract.Intents.Insert.ACTION);
contactIntent.setType(ContactsContract.RawContacts.CONTENT_TYPE);
contactIntent
.putExtra(ContactsContract.Intents.Insert.NAME, "Contact Name")
.putExtra(ContactsContract.Intents.Insert.PHONE, "123456789");
startActivityForResult(contactIntent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == 1)
{
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(this, "Added Contact", Toast.LENGTH_SHORT).show();
}
if (resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(this, "Cancelled Added Contact", Toast.LENGTH_SHORT).show();
}
}
}
}
您可以使用 button.performClick()。
如果您需要证明按钮被点击,您可以使用 my sample:
class MainActivity : AppCompatActivity() {
@SuppressLint("ClickableViewAccessibility")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.button)
// this code demonstrate success work
button.setOnClickListener {
Toast.makeText(this, "event occurred", Toast.LENGTH_LONG).show()
}
// create event with your params
var ev = MotionEvent.obtain(1L, System.currentTimeMillis(),0, button.x, button.y, 0)
val b2 = findViewById<Button>(R.id.button2)
b2.setOnClickListener {
button.onTouchEvent(ev)
}
}
}
我有代码可以将我带到保存联系人屏幕,填写我需要的详细信息,但是我现在需要它来自动按下保存联系人。有谁知道怎么做?或者如果有可能的话。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent contactIntent = new Intent(ContactsContract.Intents.Insert.ACTION);
contactIntent.setType(ContactsContract.RawContacts.CONTENT_TYPE);
contactIntent
.putExtra(ContactsContract.Intents.Insert.NAME, "Contact Name")
.putExtra(ContactsContract.Intents.Insert.PHONE, "123456789");
startActivityForResult(contactIntent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == 1)
{
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(this, "Added Contact", Toast.LENGTH_SHORT).show();
}
if (resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(this, "Cancelled Added Contact", Toast.LENGTH_SHORT).show();
}
}
}
}
您可以使用 button.performClick()。 如果您需要证明按钮被点击,您可以使用 my sample:
class MainActivity : AppCompatActivity() {
@SuppressLint("ClickableViewAccessibility")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.button)
// this code demonstrate success work
button.setOnClickListener {
Toast.makeText(this, "event occurred", Toast.LENGTH_LONG).show()
}
// create event with your params
var ev = MotionEvent.obtain(1L, System.currentTimeMillis(),0, button.x, button.y, 0)
val b2 = findViewById<Button>(R.id.button2)
b2.setOnClickListener {
button.onTouchEvent(ev)
}
}
}