我不能从广播接收器开始活动 [英] I can't start an activity from a broadcast receiver
问题描述
我从广播接收器,当它收到一个意图,我想开始/恢复活动。为指向启动活动一切正常,我得到的意图等。
I have a broadcast receiver from which, when it receives an intent, I would like to start/resume an activity. Up to point to start the activity everything works fine, I get the intent etc.
但我不能启动我的活动。
But I cannot start my activity.
下面是我的Android清单:
Here is my android manifest:
<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>
下面是我试图启动活动:
Here is the activity I'm trying to start:
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");
}
}
这是我的广播接收器,从那里我尝试启动该活动的一部分:
And here is the part of my broadcast receiver from where I try to start the activity:
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是我的主要活动的情况下,我在这里将它传递通过广播接收器的构造。我创建的主要活动的广播接收器:
I have to clarify here that webviewcontext is the context of my main activity and I pass it here through the constructor of the broadcast receiver. I create the broadcastreceiver in the main activity:
BroadcastReceiver mReceiver = new MyPhoneReceiver(context);
registerReceiver(mReceiver, filter);
但活动不会启动。这里是日志我得到:
However the activity does not start.. Here is the log I get:
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 }
任何想法更是值得欢迎的。
Any ideas are more that welcome.
感谢您提前。
推荐答案
原来,问题是上下文。
以下工作:
webviewcontext = context.getApplicationContext();
(上下文的背景下,我从得到的onReceive)
(context is the context I get from onReceive)
这篇关于我不能从广播接收器开始活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!