草庐IT

android - Android Nougat 锁屏中 URI 的 startActivity

coder 2023-12-21 原文

我正在使用从 AlarmManager 接收广播的 BroadcastReceiver。在接收器中,我开始了两项 Activity 。一个 Activity 是从这样的 URI 开始的,并且是第三方应用程序:

// Open spotify
Intent spotify = new Intent(Intent.ACTION_VIEW, Uri.parse(song));
spotify.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

try {
    context.startActivity(spotify);
} catch (ActivityNotFoundException e) {
    status = Status.SPOTIFY_NOT_INSTALLED;
}

之后,我再次使用 AlarmManager 启动属于应用程序的另一个 Activity ,延迟 5 秒:

public static void setExact(
        Context context, PendingIntent pendingIntent, long time
) {

    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
        am.setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent);
    else
        am.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);

}

public static void setExactDelay(
        Context context, PendingIntent pendingIntent, long delay
) {
    setExact(context, pendingIntent, System.currentTimeMillis() + delay);
}

    PendingIntent pendingIntent = AlarmPlayActivity.makePendingIntent(context, alarm, status, startTime);
    AlarmSet.setExactDelay(context, pendingIntent, 5000);

第二个 Activity 按预期在 5 秒后开始。但是,第一个 Activity 仅在设备解锁时开始。如果设备被锁定,它不会在 Android Nougat (7.0) 上启动。即使锁不受密码、图案等保护,情况也是如此。这曾经适用于早期的 Android 版本,即使使用安全锁也是如此。

有没有一种方法可以在不需要屏幕打开的情况下启动第一个 Activity ?

编辑:我尝试使用以下 IntentService。它在设备唤醒和解锁时有效,但在设备锁定时没有运气:

public class AlarmService extends IntentService {

    static final int NOTIFICATION_ID = 1;

    public AlarmService() {
        super("AlarmService");
    }

    public static Intent makeIntent(Context context, Alarm alarm, AlarmReceiver.Status status, long startTime) {

        Intent intent = IntentFactory.alarmPlayIntent(alarm, status, startTime);
        intent.setClass(context, AlarmService.class);

        return intent;

    }

    private static void sleep(long time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            // Nothing
        }
    }

    @Override
    protected void onHandleIntent(Intent intent) {

        // Acquire Wakelock immediately

        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);

        PowerManager.WakeLock wakeLock = pm.newWakeLock(
                PowerManager.FULL_WAKE_LOCK |
                PowerManager.ACQUIRE_CAUSES_WAKEUP |
                PowerManager.ON_AFTER_RELEASE,
                "AlarmServiceWakeLock"
        );

        wakeLock.acquire();

        KeyguardManager.KeyguardLock lock = ((KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE)).newKeyguardLock(KEYGUARD_SERVICE);
        lock.disableKeyguard();

        // Get intent data

        final Alarm alarm = IntentFactory.getAlarm(intent);
        AlarmReceiver.Status status = IntentFactory.getStatus(intent);
        final long startTime = IntentFactory.getStartTime(intent, 0);

        // Get a random song for this alarm

        AlarmDatabase db = AlarmDatabase.getInstance(this);
        Song song = db.getRandomSong(alarm);

        String songName = song == null ? "backup sound" : song.getName();

        // Start a foreground notification

        Notification notification = new NotificationCompat.Builder(this)
                .setContentTitle(getText(R.string.alarm_starting_notification_title))
                .setContentText(getString(
                        R.string.alarm_starting_notification_message, alarm.getName(), songName
                ))
                .setSmallIcon(R.drawable.ic_launcher)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .build();

        startForeground(NOTIFICATION_ID, notification);

        // Potentially open Spotify if we can

        if (song != null) {

            // Open spotify

            Intent spotify = new Intent(Intent.ACTION_VIEW, Uri.parse(song.getUri()));
            spotify.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

            try {
                startActivity(spotify);
            } catch (ActivityNotFoundException e) {
                status = AlarmReceiver.Status.SPOTIFY_NOT_INSTALLED;
            }

        } else
            status = AlarmReceiver.Status.NO_SONGS;

        // Start play activity in 10 seconds, giving Spotify some chance to load up.

        sleep(10);
        startActivity(AlarmPlayActivity.makeIntent(this, alarm, status, startTime));

        // Keep alive for 5 more seconds
        sleep(5);

        // Stop notification
        stopForeground(true);

        // Release wakelock
        wakeLock.release();


    }

}

最佳答案

我遇到了同样的问题,我认为出于安全原因,Intent.ACTION_VIEW 在您解锁屏幕之前不会工作。

讨论了相同的问题 here还。你也可以查看这个 link

关于android - Android Nougat 锁屏中 URI 的 startActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41152725/

有关android - Android Nougat 锁屏中 URI 的 startActivity的更多相关文章

  1. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  2. ruby-on-rails - Ruby 的 'open_uri' 是否在读取或失败后可靠地关闭套接字? - 2

    一段时间以来,我一直在使用open_uri下拉ftp路径作为数据源,但突然发现我几乎连续不断地收到“530抱歉,允许的最大客户端数(95)已经连接。”我不确定我的代码是否有问题,或者是否是其他人在访问服务器,不幸的是,我无法真正确定谁有问题。本质上,我正在读取FTPURI:defself.read_uri(uri)beginuri=open(uri).readuri=="Error"?nil:urirescueOpenURI::HTTPErrornilendend我猜我需要在这里添加一些额外的错误处理代码...我想确保我采取一切预防措施来关闭所有连接,这样我的连接就不是问题所在,但是我

  3. ruby - 允许主机名包含下划线的 URI.parse 的替代方法 - 2

    我正在使用DMOZ的listofurltopics,其中包含一些具有包含下划线的主机名的url。例如:608609TheOuterHeaven610InformationandimagegalleryofMcFarlane'sactionfiguresforTrigun,Akira,TenchiMuyoandotherJapaneseSci-Fianimations.611Top/Arts/Animation/Anime/Collectibles/Models_and_Figures/Action_Figures612虽然此url可以在网络浏览器中使用(或者至少在我的浏览器中可以使用:

  4. ruby - 在 Ruby 中的另一个上下文中评估潜在的相对 URI - 2

    我在Ruby程序中有两个URI。一个肯定是绝对URI,另一个可能是绝对URI或相对URI。我想在第一个的上下文中将第二个转换为绝对URI,所以如果第一个是http://pupeno.com/blog第二个是/about,结果应该是http://pupeno.com/about.有什么想法吗? 最佳答案 Ruby的内置URI和Addressablegem,做这个简短的工作。我更喜欢Addressable,因为它功能更全面,但URI是内置的。require'uri'URI.join('http://pupeno.com/blog','/

  5. ruby-on-rails - 无法捆绑安装 'open-uri' - 2

    关闭。这个问题不符合StackOverflowguidelines.它目前不接受答案。关于您编写​​的代码问题的问题必须在问题本身中描述具体问题—并且包括有效代码以重现它。参见SSCCE.org寻求指导。关闭8年前。Improvethisquestion我是Rails的新手。我正在制作一个网络应用程序,我在其中使用nokogiri搜索不同的网站以从中提取文本。所以在Gemfile中,我写了require'nokogiri'和'open-uri',但是当我捆绑安装时我得到这个错误:Couldnotfindgem'open-uri(>=0)ruby'inthegemsavailableon

  6. ruby - 为什么我必须对 Net::HTTP 请求的安全字符进行 URI.encode? - 2

    我尝试使用Net::HTTP向Twitter发送GET请求(出于隐私原因替换了用户ID):url=URI.parse("http://api.twitter.com/1/friends/ids.json?user_id=12345")resp=Net::HTTP.get_response(url)这会在Net::HTTP中引发异常:NoMethodError:undefinedmethodempty?'for#from/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/net/http.rb:1

  7. ruby - open-uri 从以 iso-8859 编码的网页返回 ASCII-8BIT - 2

    我正在使用open-uri读取一个声称以iso-8859-1编码的网页。当我读取页面内容时,open-uri返回一个以ASCII-8BIT编码的字符串。open("http://www.nigella.com/recipes/view/DEVILS-FOOD-CAKE-5310"){|f|pf.content_type,f.charset,f.read.encoding}=>["text/html","iso-8859-1",#]我猜这是因为网页中的字节(或字符)\x92不是有效的iso-8859字符。http://en.wikipedia.org/wiki/ISO/IEC_8859-

  8. ruby - 使用 open-uri 和 nokogiri 在完全加载之前读取 HTML - 2

    我正在使用open-uri和nokogiri以及ruby​​来进行一些简单的网络爬虫。有一个问题,有时html在完全加载之前就被读取了。在这种情况下,我无法获取加载图标和导航栏以外的任何内容。告诉open-uri或nokogiri等待页面完全加载的最佳方法是什么?目前我的脚本是这样的:require'nokogiri'require'open-uri'url="https://www.the-page-i-wanna-crawl.com"doc=Nokogiri::HTML(open(url,ssl_verify_mode:OpenSSL::SSL::VERIFY_NONE))puts

  9. Ruby URI - 如何在 URL 之后获取完整路径 - 2

    给定以下内容,如何获取URL的完整路径uri=URI("http://foo.com/posts?id=30&limit=5#time=1305298413")我只想要posts?id=30&limit=5#time=1305298413我试过uri.path并返回/posts和ui.query返回'id=30&limit=5' 最佳答案 您要找的方法是request_uriuri.request_uri=>"/posts?id=30&limit=5"如果需要,您可以使用任何您想要删除前导/的方法。编辑:要获取#符号后的部分,请使用

  10. ruby - URI 响应代码 - 2

    我想使用Ruby的OpenURI检查该URL是否可以正常访问。所以我想查看它的响应代码(4xx或5xx表示错误等)是否可以找到? 最佳答案 您可以使用status方法返回包含状态代码和消息的数组。require"open-uri"open("http://www.example.org")do|f|putsf.base_uri#=>http://www.example.orgputsf.status#=>["200","OK"]end 关于ruby-URI响应代码,我们在StackOve

随机推荐