I have a problem opening an activity from an appwidget. I have tried different Intent flags, PendingIntent and launch modes without any luck. I have read diffent examples here and many other places without finding the solution.
Right now when I click my button on my appwidget it opens a new activity instead of showing the instance which already exists in the app. I have posted my code below and hope you can help me.
Is there a way to find the existing activity and show it instead of creating a new one when clicking my bottom on the appwidget?
Manifest:
<application android:icon="#drawable/icon" android:label="#string/app_name" android:debuggable="true" android:name="widget.helper.ResourceHelper">
<activity android:name=".ScoreBoard"
android:label="#string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Broadcast Receiver that will process AppWidget updates -->
<receiver android:name="Widget" 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_provider" />
</receiver>
</application>
The AppWidget:
public class Widget extends AppWidgetProvider {
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews remoteViews;
ComponentName thisWidget;
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
thisWidget = new ComponentName(context, Widget.class);
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
}
#Override
public void onReceive(Context context, Intent intent)
{
super.onReceive(context, intent);
if(intent.getAction().equals("OPEN_APP")) {
Intent i = new Intent(Intent.ACTION_MAIN);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.setComponent(new ComponentName("widget","widget.ScoreBoard"));
ResourceHelper.getScoreBoard().startActivity(i);
}
}
}
set the activity launchMode to singleInstance in manifest.
Let us know if it is working.
Saneesh
Use either FLAG_ACTIVITY_REORDER_TO_FRONT or the combination of FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP. FLAG_ACTIVITY_CLEAR_TOP without FLAG_ACTIVITY_SINGLE_TOP would not work, AFAIK.
Related
I've a reciever
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent myStarterIntent = new Intent(context, MainActivity.class);
context.startActivity(myStarterIntent);
}
}
and have modified the AndroidManifest.xml, adding these lines
<receiver
android:enabled="true"
android:name=".MyBroadcastReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
to section.
The application still does not start on system boot..any ideas appreciated. At least how can I monitor what is going on after device reboot (because I cant just use breakpoints in that case)
You need to provide Intent.FLAG_ACTIVITY_NEW_TASK flag when starting activity from BroadcastReceiver.
Intent myStarterIntent = new Intent(context, MainActivity.class);
myStarterIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myStarterIntent);
This is how you can test the BroadcastReceiver.
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);
}
}
}
I tried to create clickable widget on android. I was looking for solution on this site and I found it, but finally this widget is working only on the emulator - not on real phone. This is my code:
AppWidgetProvider:
public static String WIDGET_BUTTON = "apps.test.widget.WIDGET_BUTTON";
#Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.main);
Intent clickIntent = new Intent(WIDGET_BUTTON);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.imageView1, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, views);
}
#Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (WIDGET_BUTTON.equals(intent.getAction())) {
Toast.makeText(context, "asdfsdgdasfg", Toast.LENGTH_SHORT).show();
}
}
Manifest:
<application
android:allowBackup="true"
android:icon="#drawable/image"
android:label="#string/app_name" >
<receiver
android:name=".Widget"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="apps.test.widget.WIDGET_BUTTON"/>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/test_widget_provider" />
</receiver>
</application>
Toast is showing up on the emulator but not on phone.
Ok, nevermind.
I had to reset the phone to factory settings. I don't know what was wrong.
This is a common issue,had it a few times with my phone , if the app is installed on sdcard instead of internal storage ,the widget does not appear on the list,
you can simply go to Settings->apps->find the app and then select the option to move it to internal storage from your android itself.
[http://www.technipages.com/fix-android-app-widgets-not-appearing-on-widget-list][1]
There is another way to declare in the manifest as android:externalstorage(syntax isnt correct) which works as well (I cant find the link though to the answer and I have not tried it)
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;
}
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>
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:
It is necessary that register your AppWidgetProvider in manifest.
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.
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>
I have a widget whose layout has an imageView. The view needs to be changed based on some activity. So, whenever that happens, I am calling a sendBroadcast() with the action set to UPDATE_WIDGET. However, the onReceive() in the widget code is not getting called. Below is my code.
public class TaskWidget extends AppWidgetProvider {
...
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "widget update", 2000).show();
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.searchwidget);
if (intent.getAction().equals(UPDATE_WIDGET)) {
views.setImageViewResource(R.id.newImage, R.drawable.Pic1);
}
super.onReceive(context, intent);
}
}
In the manifest,
<receiver
android:name="com.rahul.testwidget.TaskWidget"
android:label="Task Widget" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.rahul.UPDATE_WIDGET" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/widget" />
</receiver>
you have to update your widget layout
use this code just befor super.onReceive(context, intent);
ComponentName widget = new ComponentName(context, SimpleWidget.class);
AppWidgetManager.getInstance(context).updateAppWidget(widget, views);
I think your action name must be com.rahul.UPDATE_WIDGET instead of UPDATE_WIDGET.
If you are sending the broadcast from a listener on your widget you need to create a PendingIntent for a Broadcast.
PendingIntent sentPI = PendingIntent.getBroadcast(activity, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
Also, you don't seem to be able to attach 2 pending intents to a button in the widget.