1

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 ?

Community
  • 1
  • 1
  • 1
    I am assuming you are using eclipse? If so, this is what is meant to happen! It starts the app for you. Try pressing the Home button, and then test your widget. If you want to be 100%, try removing the widget code and running it, the application will still start! – edthethird Feb 05 '13 at 00:10
  • @edthethird Hi, If i open the widget and then click on it, the app doesnt open. But, it opens when i just compile/run from eclipse. – Manikandan Kandasamy Feb 05 '13 at 00:25
  • can you also post the contents of widget_layout? might be an issue there because I don't see anything off here. – edthethird Feb 05 '13 at 01:02

1 Answers1

1

I noticed that you are using PendingIntent.getService() but your calling an activity and not a service (per your manifest). Try using PendingIntent.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.

paul
  • 795
  • 1
  • 7
  • 19