Starting new Activity from Widget Click - android

My widget I've made goes onto the emulator and real device just fine with no errors, but when I go to click on the button to open a new activity on the widget, it doesn't open and all I get in Logcat is this:
10-12 12:54:23.721: I/ActivityManager(289): START u0 {dat=intent: flg=0x10000000 cmp=com.example.awesomefilebuilderwidget/.Drag_and_Drop_App bnds=[56,639][209,735] (has extras)} from pid -1
I don't understand why it doesn't open my activity (Drag_and_Drop_App.java) Here is my code in my AppProvider:
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
super.onUpdate(context, appWidgetManager, appWidgetIds);
Random r = new Random();
int randomInt = r.nextInt(1000000000);
String rand = String.valueOf(randomInt);
final int N = appWidgetIds.length;
for (int i = 0; i < N; i++){
int awID = appWidgetIds[i];
RemoteViews v = new RemoteViews(context.getPackageName(), R.layout.widget);
v.setTextViewText(R.id.tvwidgetUpdate, rand);
Intent configIntent = new Intent(context, Drag_and_Drop_App.class);
PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
v.setOnClickPendingIntent(R.id.bwidgetOpen, configPendingIntent);
appWidgetManager.updateAppWidget(awID, v);
}
}
Please note that I also have this coding in my WidgetConfig.java (the screen that first pops up when the widget is installed to collect a name for the widget):
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String e = info.getText().toString();
RemoteViews views = new RemoteViews(c.getPackageName(), R.layout.widget);
views.setTextViewText(R.id.tvConfigInput, e);
Intent in = new Intent(c, Drag_and_Drop_App.class);
in.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, awID);
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Make the pending intent unique...
in.setData(Uri.parse(in.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent pi = PendingIntent.getActivity(c, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.bwidgetOpen, pi);
awm.updateAppWidget(awID, views);
Intent result = new Intent();
result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, awID);
setResult(RESULT_OK, result);
finish();
}
}
and if it helps any, here is my manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.awesomefilebuilderwidget"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name="com.example.awesomefilebuilderwidget.AFBWidget" android:label="#string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="#xml/widget_stuff"/>
</receiver>
<activity android:name="com.example.awesomefilebuilderwidget.WidgetConfig" android:label="#string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
</intent-filter>
</activity>
<activity android:name="com.example.awesomefilebuilderwidget.Drag_and_Drop" android:label="#string/app_name"></activity>
</application>
</manifest>

Related

AppWidget with collection - can`t run Config Activity when clicking on widget

I have got the issue that is very popular on Stackoverflow and there are a lot of recipes how to fix it. But unfortunately, all of them don`t fit me.
I created appWidget with collection (StackView), added ConfigActivity for configuration and permanent update mechanism with assigned time interval. Widget is displayed in a proper way, update works fine but when I want to run Config Activity and click on widget - nothing happens!
Maybe, I've missed some important details and hope you'll suggest me. Here are some of code snippets:
Manifest
<uses-sdk android:minSdkVersion="11"
android:targetSdkVersion="22"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:name=".FinApp"
android:allowBackup="true"
android:icon="#mipmap/ic_fin"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name=".widget.WidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/fin_app_widget_info" />
</receiver>
<activity android:name=".ConfigActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
<service
android:name=".widget.WidgetService"
android:permission="android.permission.BIND_REMOTEVIEWS"/>
</application>
AppWidgetProvider
public class WidgetProvider extends AppWidgetProvider {
private static final String TAG = WidgetProvider.class.getSimpleName();
#Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "onReceive");
super.onReceive(context, intent);
}
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Log.w(TAG, "onUpdate: appWidgetIds - " + Arrays.toString(appWidgetIds));
for (int id : appWidgetIds) {
updateAppWidgetInstance(context, appWidgetManager, id);
}
}
public static void updateWidgetsOnDataSetChanged (Context context) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(
new ComponentName(context.getPackageName(), WidgetProvider.class.getName()));
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.widget_list);
}
private static void addWidgetListener(Context context, int appWidgetId, RemoteViews widget) {
Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent runConfigActivity = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
widget.setOnClickPendingIntent(R.id.widget_container, runConfigActivity);
}
private static void setupWidgetService(Context context, int appWidgetId, RemoteViews widget) {
Intent adapterIntent = new Intent(context, WidgetService.class);
adapterIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
adapterIntent.setData(Uri.fromParts("content", String.valueOf(appWidgetId), null));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
widget.setRemoteAdapter(R.id.widget_list, adapterIntent);
} else {
//noinspection deprecation
widget.setRemoteAdapter(appWidgetId, R.id.widget_list, adapterIntent);
}
}
private static void updateAppWidgetInstance(Context context, AppWidgetManager appWidgetManager,
int appWidgetId) {
Log.e(TAG, "updateAppWidgetInstance");
RemoteViews widget = new RemoteViews(context.getPackageName(), R.layout.widget_collection_layout);
setupWidgetService(context, appWidgetId, widget);
addWidgetListener(context, appWidgetId, widget);
appWidgetManager.updateAppWidget(appWidgetId, widget);
}
}
widget_collection_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/widget_container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<StackView
android:id="#+id/widget_list"
android:loopViews="true"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
As I understand the point is in the method addWidgetListener
private static void addWidgetListener(Context context, int appWidgetId, RemoteViews widget) {
Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent runConfigActivity = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
widget.setOnClickPendingIntent(R.id.widget_container, runConfigActivity);
}
but I have no idea what's wrong...
After some investigation I came to a conclusion that if I use collection in appWidget and want to open Config Activity tapping on whole widget container that the best practice is using setPendingIntentTemplate method, even if you dont't want to handle item click but the whole list instead (list container).
private static void addWidgetListener(Context context, int appWidgetId, RemoteViews widget) {
Intent intent = new Intent(context, ConfigActivity.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
PendingIntent runConfigActivity = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
widget.setPendingIntentTemplate(R.id.widget_list, runConfigActivity);
}
and add 2 lines in WidgetFactory in method getViewAt(int position)
Intent intent = new Intent();
views.setOnClickFillInIntent(R.id.widget_item_container, intent);

Basic widget: listed but cannot be added to homescreen

I am for the first time checking how to make a widget app. The first thing I want to do is add a widget to the home screen, a basic textview with a static text. Nothing else.
After reading the guide I tried to construct one. I get to the point that the widget is listed by the system, I run normally the configuration activity. But when I drag and drop the widget to the homescreen, nothing happens. I don't know where to debug, when I do debug my Widgetprovider class all looks fine (resources are not null, view gets updated) so I don't know where to look for an error message.
What is wrong?
At least some direction on where to look would be very appreciated!
Here's the manifest file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tizanacode.ExJakdojade">
<application android:allowBackup="true"
android:label="#string/app_name"
android:icon="#drawable/ic_launcher"
android:theme="#style/AppTheme">
<activity
android:name="WidgetConfigurationActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:icon="#drawable/ic_launcher"
android:label="Watched Line"
android:name="ExJakdoJade" >
<intent-filter >
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/ex_jakdojade_info" />
</receiver>
</application>
Here is the provider class:
public class ExJakdoJade extends AppWidgetProvider {
private static final String ACTION_CLICK = "ACTION_CLICK";
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
// Get all ids
ComponentName thisWidget = new ComponentName(context,
ExJakdoJade.class);
/*int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for (int widgetId : allWidgetIds) {*/
for (int widgetId : appWidgetIds) {
// Create some random data
int number = (new Random().nextInt(100));
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.layout);
Log.w("WidgetExample", String.valueOf(number));
// Set the text
remoteViews.setTextViewText(R.id.update, String.valueOf(number));
// Register an onClickListener
Intent intent = new Intent(context, ExJakdoJade.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.update, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
}

Android Widget Button Click Toast Maessage

I've created a simple widget have only a button and textView. When I add my widget first time to the homescreen a FC message coming and widget will added. And button click toast message won't work. Only i need to have a toast message when clicking the widget button. I've gone through lots of tutorials but couldn't figure out whats the problem. Any genius who'd like to help..really appriciate the help..Thanks..
widgetProvider.java
public class widgetProvider extends AppWidgetProvider{
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
Intent intent = new Intent(context, widgetProvider.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
views.setOnClickPendingIntent(R.id.button1, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_UPDATE)) {
Toast.makeText(context, "Button Clicked", Toast.LENGTH_SHORT).show();
}
}
}
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name=".widgetProvider" android:label="#string/app_name">
<intent-filter >
<action
android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/widgetprovider" />
</receiver>
</application>
your onUpdate is never called since you overriden the onReceive method.

Android widget using a config activity wont update

I am making an Android widget to display real time transportation information based on bus stop numbers. It displays the next 3 bus arrivals for the stop number and has a button where the user can refresh the data. That was working ok until I tried adding my widget configuration activity.
Now when you select the widget, a configuration screen is opened where you enter your bus stop number. Once you do that the widget is added to the home screen, but it will not execute the method to gather data and the refresh button will not work for that either. I have not been able to figure out why.
WidgetConfig
public class WidgetConfig extends Activity implements OnClickListener{
EditText info;
AppWidgetManager awm;
Context c;
int awID;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.widget_config);
Button b = (Button)findViewById(R.id.bWidgetConfig);
b.setOnClickListener(this);
c = WidgetConfig.this;
//getting info about the widget that launched this activity
Intent i = getIntent();
Bundle extras = i.getExtras();
if (extras != null){
awID = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
}else{
finish();
}
awm = AppWidgetManager.getInstance(c);
}
public void onClick(View v) {
// TODO Auto-generated method stub
info = (EditText)findViewById(R.id.etWidgetConfig);
String e = info.getText().toString();
RemoteViews views = new RemoteViews(c.getPackageName(), R.layout.widget_layout);
views.setTextViewText(R.id.tvStopNum, e);
Intent in = new Intent(c, WidgetConfig.class);
PendingIntent pi = PendingIntent.getActivity(c, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.tvStopNum, pi);
awm.updateAppWidget(awID, views);
Intent result = new Intent();
result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, awID);
setResult(RESULT_OK, result);
finish();
}
AppWidgetProvider
public class MyWidgetProvider extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
// Create a new intent that will target this class
Intent intent = new Intent(context, MyWidgetProvider.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[0]);
intent.setAction("update");
// Create a new PendingIntent which will be run whenever the refresh button is pressed
// This PendingIntent will run the intent we just created before this
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.button1, pi);
// Manually run the AsyncTask to initially populate the 5 question fields
new RTPIinformation().execute(context);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
// This method is called when it receives an Intent. In particular, it will
// receive an intent whenever the refresh button is clicked
#Override
public void onReceive(Context context, Intent intent){
super.onReceive(context, intent);
if (intent.getAction().equals("update")){
new RTPIinformation().execute(context);
}
}
class RTPIinformation extends AsyncTask<Context, Void, List<RealtimeData>>{
.
.
.
}
Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ruben.dublin.bus.widget"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".WidgetConfig"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
<receiver
android:name=".MyWidgetProvider"
android:label="#string/app_name">
<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>
Any help would be greatly appreciated

Widget has a weird bahaviour

I have developed an application that makes use of a widget. The widget could be added in the homescreen and used as a button. I have recently downgraded the version to <uses-sdk android:minSdkVersion="7" /> and no errors were spotted. However, now when i reinstall the, the widget is not responsive. In addition when I try to install the app on another phone running android 4.0.3, widget is not available anywhere, so i cannot even add it to the homescreen. Here is my code for the widget:
public class MyWidget extends AppWidgetProvider {
#Override
public void onUpdate(Context c, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
final int N = appWidgetIds.length;
//for each of the instances of the widget
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
RemoteViews views = new RemoteViews(c.getPackageName(), R.layout.widget_layout);
Intent fireDialog = new Intent(c,Execute.class);
PendingIntent pendingIntent = PendingIntent.getActivity(c, 0, fireDialog, 0);
views.setOnClickPendingIntent(R.id.myButton, pendingIntent); //when the button is clicked, an activity is launched
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
#Override
public void onReceive(Context c, Intent intent)
{
final String action=intent.getAction();
if(AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)){
final int appWidgetId=intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,AppWidgetManager.INVALID_APPWIDGET_ID);
if(appWidgetId!=AppWidgetManager.INVALID_APPWIDGET_ID){
this.onDeleted(c,new int[]{appWidgetId});
}
}else{
super.onReceive(c, intent);
}
}
}
Here is the manifest.xml:
<application>
<receiver android:name=".myButton"
android:icon="#drawable/icon"
android:label="myWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="#xml/my_xml" />
</receiver>
</application>

Categories

Resources