gpt4 book ai didi

android - BroadCastReceiver 中的 PhoneStateListener

转载 作者:搜寻专家 更新时间:2023-11-01 09:41:21 29 4
gpt4 key购买 nike

我正在制作一个应用程序,该应用程序将通过警报管理器触发拨出电话。此外,应该监控去电,以检查调用是否成功、忙碌或其他任何情况。我的问题是如何在 BroadCastReceiver 中调用 PhoneStateListener?

主要 Activity

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void startAlert(View view) {
EditText text = (EditText) findViewById(R.id.time);
int i = Integer.parseInt(text.getText().toString());
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (i * 1000), pendingIntent);
}
}

广播接收器

public class MyBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

PackageManager pm = context.getPackageManager();
int hasPerm = pm.checkPermission(android.Manifest.permission.PROCESS_OUTGOING_CALLS,context.getPackageName());
if (hasPerm != PackageManager.PERMISSION_GRANTED) {
Intent myIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "+38163670832"));;
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}
}

我想应用的 PhoneStateListener 示例

public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_OFFHOOK:
//Kada se korisnik javi
Toast.makeText(context, "Veza uspostavljena", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_IDLE:
//Idle stanje
Toast.makeText(context, "Idle mod", Toast.LENGTH_LONG).show();
break;
}
}

最佳答案

how to call for PhoneStateListener in BroadCastReceiver?

嗯……为了什么? BroadCastReceiver 本身可以为您提供有关当前电话状态的所有必要信息,因此您不需要任何其他信息

public class MyBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
final String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

/* Incoming call */
if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) {
} else

/* Outgoing call */
if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(state)) {
} else

/* Call ended */
if (TelephonyManager.EXTRA_STATE_IDLE.equals(state)) {
}
}

}

关于android - BroadCastReceiver 中的 PhoneStateListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39719137/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com