0

In my application, I have a Fragment which is attached to the main activity. This Fragment displays data with the help of an adapter. In the adapter, I have inflated a layout which has a clickable text "Watch From Home". The adapter displays data from the server. I want to show the same data on the home screen and when I click on the "Watch From Home" text, a home screen widget should be created without any user interaction.

I made a receiver MyWidgetProvider.What should the method onClickText() for "Watch From Home" have so that it takes me to the widget? I must admit, I am new to android. Thanks

Android Manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pack.android.receiver"
    android:versionCode="1"
    android:versionName="1.0" >

    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name" >
        <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>
    </application>

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

</manifest> 

MyWidgetProvider.Java file

public class MyWidgetProvider extends AppWidgetProvider {

    private TextView team1Name;
    private TextView team2Name;
    private TextView team1Score;
    private TextView team2Score;

    public static boolean widgetView=false;
      private static final String LOG = "com.playup.android.receiver";

      public MyWidgetProvider(TextView team1Name, TextView team2Name, TextView team1Score, TextView team2Score){
          this.team1Name=team1Name;
          this.team2Name=team2Name;
          this.team1Score=team1Score;
          this.team2Score=team2Score;
          initializeViews();
      }

      @Override
      public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {

        Log.w(LOG, "onUpdate method called");
        // Get all ids
        ComponentName thisWidget = new ComponentName(context,MyWidgetProvider.class);
        int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

        // Build the intent to call the service
        Intent intent = new Intent(context.getApplicationContext(), UpdateWidgetService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);

        // Update the widgets via the service
        context.startService(intent);
      }

      public void initializeViews(){
    //    team1Name= (TextView)content_layout.findViewById(R.id.team1Name);

      }
} 

UpdateWidgetService

public class UpdateWidgetService extends Service {
  private static final String LOG = "com.playup.android.receiver";

  @Override
  public void onStart(Intent intent, int startId) {
    Log.i(LOG, "Called");
    // Create some random data

    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this
        .getApplicationContext());

    int[] allWidgetIds = intent
        .getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);

    ComponentName thisWidget = new ComponentName(getApplicationContext(),
        MyWidgetProvider.class);
    int[] allWidgetIds2 = appWidgetManager.getAppWidgetIds(thisWidget);
    Log.w(LOG, "From Intent" + String.valueOf(allWidgetIds.length));
    Log.w(LOG, "Direct" + String.valueOf(allWidgetIds2.length));

    for (int widgetId : allWidgetIds) {
      // Create some random data
      int number = (new Random().nextInt(100));

      RemoteViews remoteViews = new RemoteViews(this
          .getApplicationContext().getPackageName(),0x7f030061);        //Since R could not be resolve, I used the generated ids
      Log.w("Widget", String.valueOf(number));
      // Set the text
      remoteViews.setTextViewText(0x7f0a022a,"Random: " + String.valueOf(number));

      // Register an onClickListener
      Intent clickIntent = new Intent(this.getApplicationContext(),MyWidgetProvider.class);

      clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
      clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,
          allWidgetIds);

      PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, clickIntent,PendingIntent.FLAG_UPDATE_CURRENT);
      remoteViews.setOnClickPendingIntent(0x7f0a022a, pendingIntent);
      appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }
    stopSelf();

    super.onStart(intent, startId);
  }

  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }
Kara
  • 6,115
  • 16
  • 50
  • 57
Feona
  • 295
  • 2
  • 6
  • 18

3 Answers3

1

Simply saying, You can't create a home widget without any user interaction.


Update:

Your receiver tag should start like this:

<receiver
    android:name=".MyWidgetProvider" 
    android:label="My widget label" >
    <!-- The rest as is -->

</receiver>
iTurki
  • 16,292
  • 20
  • 87
  • 132
  • okay, let's say we have user interaction. When I run my app, my widget doesn't get listed in the widgets on the desktop. How to bring the widget available there. please help. – Feona Sep 06 '12 at 07:33
  • Did you [declare your app widget in the manifest](http://developer.android.com/guide/topics/appwidgets/index.html#Manifest)? – iTurki Sep 06 '12 at 07:35
  • You mean by `doesn't get listed on the desktop` that it didn't show in the phone's widget list? – iTurki Sep 06 '12 at 09:21
1

Home Screen Widget can only add to Home Screen directly by user.Only user can add widget as guest to Home Screen or other applications.

Edit:

To see your widget in widgets list:

  1. It is necessary that register your AppWidgetProvider in manifest.
  2. Your App has to have Launcher/Main Activity,and App must be directly run by user,before it be visible in widgets list.This is necessary ,because no broadcastreceiver(and so no AppWidgetProvider),no service,... of your App could not register before your App run directly by user.If you look at your manifest,you will see that your App has no Launcher/Main Activity(it has only a receiver),so it can not run by user and your AppWidgetProvider(that is a broadcastreceiver) will not register and thereupon you can not see your widget in widget list.
hasanghaforian
  • 13,858
  • 11
  • 76
  • 167
0

This had me scratching my head for a while. I had the meta-data tag as child of the intent-filter tag, which is wrong. The meta-data is to be child of the receiver tag.

Incorrect version

 <receiver
            android:name=".AppWidgetReceiver"
            android:label="AppWidgetReceiver" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />

                <meta-data
                    android:name="android.appwidget.provider"
                    android:resource="@xml/appwidget_info" />
            </intent-filter>
        </receiver>

Following is the correct version

<receiver android:name=".AppWidgetReceiver"
            android:label="AppWidgetReceiver" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/appwidget_info" />
        </receiver>
Thupten
  • 2,158
  • 1
  • 24
  • 31