【问题标题】:I can't start an activity from a broadcast receiver我无法从广播接收器开始活动
【发布时间】:2012-11-16 12:32:55
【问题描述】:

我有一个广播接收器,当它接收到一个意图时,我想开始/恢复一个活动。到开始活动为止一切正常,我明白了意图等。

但我无法开始我的活动。

这是我的 android 清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.imgzine.testing"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".WebViewActivity"
        android:theme="@android:style/Theme.NoTitleBar" >
    </activity>
    <activity
        android:name="com.imgzine.testing.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

这是我要开始的活动:

package com.imgzine.testing;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;

public class WebViewActivity extends Activity {

private WebView webView;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);

    Log.w("DEBUG", "Hello, I'm a webview");

    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("http://www.google.com");

}

}

这是我尝试开始活动的广播接收器的一部分:

                    Log.w("DEBUG", "Hello, I receive.");
        Intent webviewintent = new Intent(webviewcontext,
        WebViewActivity.class);
        webviewintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        webviewintent.setAction(Intent.ACTION_MAIN);
        webviewintent.addCategory(Intent.CATEGORY_LAUNCHER);
        webviewcontext.startActivity(webviewintent);

我必须在这里澄清 webviewcontext 是我的主要活动的上下文,我通过广播接收器的构造函数将它传递到这里。我在主要活动中创建广播接收器:

 BroadcastReceiver mReceiver = new MyPhoneReceiver(context);
 registerReceiver(mReceiver, filter);

但是活动没有开始。这是我得到的日志:

11-16 12:47:36.144: W/DEBUG(818): Hello, I receive.
11-16 12:47:36.144: I/ActivityManager(58): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.imgzine.testing/.WebViewActivity }

欢迎任何想法。

提前谢谢你。

【问题讨论】:

    标签: android broadcastreceiver start-activity


    【解决方案1】:
    Intent intent = new Intent(webviewcontext,WebViewActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    webviewcontext.startActivity(intent);
    

    【讨论】:

      【解决方案2】:

      原来问题出在上下文。

      以下作品:

      webviewcontext = context.getApplicationContext();
      

      (上下文是我从 onReceive 得到的上下文)

      【讨论】:

        猜你喜欢
        • 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
        相关资源
        最近更新 更多