I am trying to open an activity if the widget is clicked. But, when i upload the package into my emulator, the activity opens automatically, even though it should open only if i click on the widget.
This is my manifest file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.NewsWidget"
android:versionCode="1"
android:versionName="1.0" >
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<service android:name=".UpdateWidgetService" >
</service>
<receiver android:name="MyWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_info" />
</receiver>
<activity
android:name="com.example.NewsWidget.MainActivity"
android:label="@string/app_name"
android:launchMode="singleInstance" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DetailedNewsViewer"
android:label="DetailedNewsViewer" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
I have added the following code into my widget provider using the details from another question here (Launching activity from widget).
Intent i = new Intent();
i.setClassName("com.example.NewsWidget", "com.example.NewsWidget.MainActivity");
PendingIntent myPI = PendingIntent.getService(context, 0, i, 0);
//intent to start service
// Get the layout for the App Widget
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
//attach the click listener for the service start command intent
views.setOnClickPendingIntent(R.id.update, myPI);
//define the componenet for self
ComponentName comp = new ComponentName(context.getPackageName(), MyWidgetProvider.class.getName());
//tell the manager to update all instances of the toggle widget with the click listener
mgr.updateAppWidget(comp, views);
What is the problem ?
I noticed that you are using
PendingIntent.getService()but your calling an activity and not a service (per your manifest). Try usingPendingIntent.getActivity()instead and see if your pendingIntent fires.Your main activity is loading when you run from eclipse, but edthethird is right, that is probably not related to your problem. It is just the default eclipse behavior to launch your main activity.