【问题标题】:Is it possible to start an intent from a broadcast receiver?是否可以从广播接收器开始意图?
【发布时间】:2011-01-28 20:52:38
【问题描述】:

我正在尝试在包含地图坐标的 SMS 到达时启动地图应用程序。我的广播接收器设置没有问题,据我了解,我需要执行以下操作:

                Uri uri = Uri.parse("geo:" + latitude  + "," + longitude +"?z=10");
                Intent mapintent = new Intent(android.content.Intent.ACTION_VIEW, uri);
                startActivity(mapintent);

但是 Eclipse 告诉我 startActivity 没有为 BroadcastReceiver 类型定义。如何开始意图?

【问题讨论】:

    标签: android google-maps android-intent broadcastreceiver


    【解决方案1】:

    startActivity() 在 Context 类上,您只需要使用 Context 的实例。你会在onReceive() 方法中得到一个传递。

    【讨论】:

    • 我尝试过使用 context.startActivity(intent) 但应用程序强制以 java.lang.StringIndexOutOfBoundsException 关闭:字符串索引超出范围:4 错误导致:java.lang.StringIndexOutOfBoundsException:字符串超出范围的索引:4
    • 解决了那个问题,还需要添加mapintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    【解决方案2】:

    为了完整性:

    正如 mbaird(和 Eugene)所说,使用 OnCreate() 中传递的活动的上下文 - 最终解决方案如下所示:

    Uri uri = Uri.parse("geo:" + latitude  + "," + longitude +"?z=10");
    
    Intent mapintent = new Intent(android.content.Intent.ACTION_VIEW, uri);
    
    mapintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    
    context.startActivity(mapintent);
    

    【讨论】:

      【解决方案3】:

      你应该在某个地方有一个 Context 的实例。上下文具有您可以使用的 startActivity 方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多