I mean I am able to create widget on the home screen successfully.
When I will click on the widget, it will work fine
in the below cases.
1) When widget is created newly in portrait mode.
2) When widget is created newly in Landscape mode
But once I will switch to Landscape from Portrait mode the widget
which was created in Portrait will not work, I mean when I clicked on
the widget it is not responding.
Similarly when I will switch to Portrait from Landscape mode the
widget which was created in Landscape is not responding.
Below is my code.
import java.util.LinkedList;
import java.util.Queue;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.os.IBinder;
import android.widget.RemoteViews;
public class UpdateService extends Service {
private static final String TAG = "UpdateService";
private static Queue<Integer> sAppWidgetIds = new LinkedList<Integer>
();
public static final String ACTION_UPDATE_ALL =
"com.lge.android.memo.UPDATE_ALL";
public static void requestUpdate(int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
sAppWidgetIds.add(appWidgetId);
}
}
private static boolean hasMoreUpdates() {
boolean hasMore = !sAppWidgetIds.isEmpty();
if (!hasMore) {
}
return hasMore;
}
private static int getNextUpdate() {
if (sAppWidgetIds.peek() == null) {
return AppWidgetManager.INVALID_APPWIDGET_ID;
} else {
return sAppWidgetIds.poll();
}
}
@Override
public void onStart(Intent intent, int startId) {
AppWidgetManager appWidgetManager =
AppWidgetManager.getInstance(this);
while (hasMoreUpdates()) {
int appWidgetId = getNextUpdate();
RemoteViews updateViews = null;
updateViews =
MemoAppWidgetProvider.buildUpdate(this,
appWidgetId);
if (updateViews != null) {
appWidgetManager.updateAppWidget(appWidgetId,
updateViews);
}
}
stopSelf();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
import java.io.File;
import com.lge.android.memo.Memo.Memos;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.RemoteViews;
// Need the following import to get access to the app resources, since
this
// class is in a sub-package.
public class MemoAppWidgetProvider extends AppWidgetProvider {
// log tag
private static final String TAG = "MemoAppWidgetProvider";
public void onUpdate(Context context, AppWidgetManager
appWidgetManager, int[] appWidgetIds) {
Log.d(TAG, "onUpdate");
if (appWidgetIds == null) {
appWidgetIds = appWidgetManager.getAppWidgetIds(
new ComponentName(context,
MemoAppWidgetProvider.class));
}
// Request update for these widgets and launch updater service
UpdateService.requestUpdate(appWidgetIds);
context.startService(new Intent(context,
UpdateService.class));
}
public void onDeleted(Context context, int[] appWidgetIds) {
Log.d(TAG, "onDeleted");
// When the user deletes the widget, delete the preference
associated with it.
final int N = appWidgetIds.length;
Cursor widgetID = null;
File fileDir = null;
String memoTitle = "";
for (int i=0; i<N; i++) {
System.out.println("WID ID "+appWidgetIds[i]);
widgetID = context.getContentResolver().query(Uri.parse
("content://com.lge.provider.Memo/memos"),
MemoList.PROJECTION, Memo.Memos.MEMOWIDGETID
+"="+appWidgetIds[i],
null,
Memos.DEFAULT_SORT_ORDER);
widgetID.moveToFirst();
try {
memoTitle = widgetID.getString(5);
System.out.println("HHHH ## "+memoTitle);
fileDir = new File("/data/data/com.lge.android.memo/
files/"+memoTitle+".png");
if(fileDir.exists()) {
System.out.println("FILE NAME "+fileDir.getName());
System.out.println("FILE IS DELETED
"+fileDir.delete());
}
} catch (Exception e) {
//Ignore
}
context.getContentResolver().delete(
Uri.parse("content://com.lge.provider.Memo/memos"),
Memo.Memos.MEMOWIDGETID+"="+appWidgetIds[i],
null);
}
if(widgetID != null) {
widgetID.close();
}
}
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
Bundle extras=intent.getExtras();
if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action))
{
final int appWidgetId = extras.getInt
(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID)
{
this.onDeleted(context, new int[] { appWidgetId });
}
}else if(AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals
(action)) {
final int appWidgetId = extras.getInt
(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
if (appWidgetId !=
AppWidgetManager.INVALID_APPWIDGET_ID) {
this.onUpdate
(context,AppWidgetManager.getInstance(context), new int[]
{ appWidgetId });
}
else {
super.onReceive(context, intent);
}
}
}
public static RemoteViews buildUpdate(Context context,int id) {
Log.d(TAG, "Building medium widget update");
Intent detailIntent = new Intent(context, MemoList.class);
detailIntent.setAction(String.valueOf(id));
detailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
final String[] PROJECTION = new String[] {
Memos._ID, // 0
Memos.MEMO, // 1
};
String title;
Cursor cursor = context.getContentResolver().query(Uri.parse
("content://com.lge.provider.Memo/memos"), PROJECTION,
Memo.Memos.MEMOWIDGETID+"="+id, null,
Memos.DEFAULT_SORT_ORDER);
if(cursor.getCount()==0)
{
title="Create Memo";
}
else
{
cursor.moveToFirst();
String text=cursor.getString(cursor.getColumnIndex
(Memo.Memos.MEMO)).trim();
int len_text=text.length();
title=text.substring(0, len_text>=6? 6 : len_text);
}
PendingIntent pending = PendingIntent.getActivity(context,
id , detailIntent ,0);
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.appwidget_provider);
views.setOnClickPendingIntent(R.id.memo_full, pending);
views.setTextViewText(R.id.appwidget_text, title);
return views;
}
}
Thanks in advance
On Jul 8, 9:05 pm, Jack Ha <[email protected]> wrote:
> What do you mean the widget is not able to launch? Please post your
> source code here if possible.
>
> --
> Jack Ha
> Open Source Development Center
> ・T・ ・ ・Mobile・ stick together
>
> The views, opinions and statements in this email are those of
> the author solely in their individual capacity, and do not
> necessarily represent those of T-Mobile USA, Inc.
>
> On Jul 8, 1:49 am, sunita <[email protected]> wrote:
>
> > I have a widget , thats working just fine in a particular orientation,
> > but the moment i change the orientation to landscape/portrait, the
> > widget is not able to launch. I have one layout and one layout-land
> > folder in res directory.The problem is that the widget created in
> > landscape mode is not opening in portrait mode and vice-versa.
>
> > Thanks for the answer
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---