即使应用程序在特定时间关闭,也可以打开电话拨号器

问题描述

即使我的应用程序已关闭,我也想在特定时间从我的应用程序打开 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 的更好的解决方案。谢谢

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...