问题
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.
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);
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.
Thank you in advance.
回答1:
Intent intent = new Intent(webviewcontext,WebViewActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
webviewcontext.startActivity(intent);
回答2:
It turns out that the problem was the context.
The following works:
webviewcontext = context.getApplicationContext();
(context is the context I get from onReceive)
来源:https://stackoverflow.com/questions/13416656/i-cant-start-an-activity-from-a-broadcast-receiver