问题
我遇到了第一个世界问题,我想加快/消除这个问题。我的车在带门的停车场中,有两个门(1个行人-钥匙通道,和1个用于进出汽车的门)。通过拨打特定的电话号码可以打开轿门。
当前流程
我打了一个特定的电话号码(该号码将我的号码识别为打开门的有效客户),电话一接到电话,便打开了门。
建议的解决方案
一个应用程序,该应用程序会在加载时调用指定的号码,并且一旦另一侧接听了出站呼叫(可能等待1秒钟);挂断电话。
进展日期
到目前为止,我有一个可以很好地打开该号码的应用程序-但我似乎无法弄清楚如何监听该呼出电话,并在接听电话后挂断了电话-我觉得我相当关闭,但现在已经使用了太多资源,以至于我又一次迷失了。
代码
//清单
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
//主要活动
public class MainActivity extends AppCompatActivity {
private MyPhoneCallListener mListener;
TelephonyManager mTelephonyManager;
private static final int MY_REQUIRED_PERMISSIONS = 1;
TextView textview;
String phoneNumber = "tel: 5554"; // Calling an AVD for testing
private void makeCall(){
// Permission was granted.
textview.setText(textview.getText() + "\nPermissions Granted.");
// Register the PhoneStateListener to monitor phone activity.
mListener = new MyPhoneCallListener();
mTelephonyManager.listen(mListener,
PhoneStateListener.LISTEN_CALL_STATE);
// Create the intent.
Intent callIntent = new Intent(Intent.ACTION_CALL);
// Set the data for the intent as the phone number.
callIntent.setData(Uri.parse(phoneNumber));
// If package resolves to an app, check for phone permission,
// and send intent.
if (callIntent.resolveActivity(getPackageManager()) != null) {
startActivity(callIntent);
} else {
Log.e("shane-", "Can't resolve app for ACTION_CALL Intent.");
}
}
private boolean isTelephonyEnabled() {
if (mTelephonyManager != null) {
if (mTelephonyManager.getSimState() ==
TelephonyManager.SIM_STATE_READY) {
return true;
}
}
return false;
}
private boolean checkForPhonePermission() {
boolean permissionsGranted = false;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_DENIED
|| checkSelfPermission(Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_DENIED
|| checkSelfPermission(Manifest.permission.PROCESS_OUTGOING_CALLS) == PackageManager.PERMISSION_DENIED) {
String[] permissions = {
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.CALL_PHONE,
Manifest.permission.PROCESS_OUTGOING_CALLS
};
requestPermissions(permissions, MY_REQUIRED_PERMISSIONS);
} else {
permissionsGranted = true;
}
}
return permissionsGranted;
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
Log.d("shane-", Integer.toString(grantResults[0]));
switch (requestCode) {
case MY_REQUIRED_PERMISSIONS: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
makeCall();
} else {
// Permission denied. Stop the app.
Log.d("shane-", "Permission was denied");
textview.setText(textview.getText() + "\nYou have denied the required permissions for this app to work.");
}
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview = (TextView) findViewById(R.id.tv);
// Create a telephony manager.
mTelephonyManager = (TelephonyManager)
getSystemService(TELEPHONY_SERVICE);
if (isTelephonyEnabled()) {
Log.d("shane-", "Telephony Enabled");
textview.setText(textview.getText() + "\nTelephony enabled");
Toast.makeText(this,
"Telephony is enabled :)",
Toast.LENGTH_LONG).show();
// Check for phone permission.
if(checkForPhonePermission()){
makeCall();
}
// else telephony not enabled
} else {
Log.d("shane-", "Telephony not enabled :(");
textview.setText(textview.getText() + "\nTelephony not enalbed");
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (isTelephonyEnabled()) {
mTelephonyManager.listen(mListener,
PhoneStateListener.LISTEN_NONE);
}
}
}
// MyPhoneListener.java
class MyPhoneCallListener extends PhoneStateListener {
private String LOG_TAG = "shane-phonelistener";
private boolean wasRinging;
@Override
public void onCallStateChanged(int state, String incomingNumber) {
Log.i(LOG_TAG, Integer.toString(state));
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
Log.i(LOG_TAG, "RINGING");
wasRinging = true;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i(LOG_TAG, "OFFHOOK");
if (!wasRinging) {
// Start your new activity
} else {
// they answered the call
// Cancel your old activity
Log.i(LOG_TAG, "ANSWERED THE CALL"); // never gets hit
}
// this should be the last piece of code before the break
wasRinging = true;
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.i(LOG_TAG, "IDLE");
if (wasRinging) {
// Call was rejected
Log.i(LOG_TAG, "REJECTED THE CALL");
}
// this should be the last piece of code before the break
wasRinging = false;
break;
}
}
}
道歉,老实说,这很容易。但它出现在Android 23之后,执行此类操作变得更加困难。
预先感谢您的帮助!