问题描述
即使我的应用程序已关闭,我也想在特定时间从我的应用程序打开 android 电话拨号程序。
我为此尝试了 AlarmManager -
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this,CallSchaduleBroadcast.class);
intent.putExtra("PHONE_NUMBER",edtPhone.getText().toString());
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,intent,0);
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC,timeInMillis,pendingIntent);
这是我在 CallSchaduleBroadcast 类中的 onReceive 方法-
String number = intent.getStringExtra("PHONE_NUMBER");
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:" + number));
context.startActivity(callIntent);
但这给了我以下错误-
Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
我正在构建我的应用程序 -
minSdkVersion 21
targetSdkVersion 29
这是完成我的工作的正确方法还是我需要尝试其他东西,例如 Job Scheduler 或 Work Manager。 请帮助我,提前致谢
解决方法
糟糕,我得到了解决方案
int main()
{
// Start with the empty list
struct Node* head = NULL;
int i;
// Insert through a for loop
for (i = 0; i < 5; i++)
AddtoHead(&head,i+10);
printf("hello %d times!\n",i);
PrintFList(head); // --> Segmentation fault!
}
我只需要在 callIntent 中将Flags 设置为FLAG_ACTIVITY_NEW_TASK。
但我仍然想知道是否有使用 Job Scheduler 或 Work Manager 的更好的解决方案。谢谢