Can an Activity be started from within a WidgetAppProvider? - android

I have a widget that I have an intent that starts an activity when I click, but I need to get saved Data into the widget, so onEnabled can I start an Activity that will look up the saved data for the activity?
Also an intent I need to set up for the click events... how do I set these up with out first having the config show up?

I still do not know if an activity can be explicitly started without doing something like an onclickevent... but saving data within the widget can be accomplished by using the
Context.getSharedPreferences( String name, int mode );
to get saved prefernces, and by using
nt[] allids = AppWidgetManager .getInstance(context) .
getAppWidgetIds(new ComponentName(context, AwarenessWidget.class));
to get all the IDS

Related

Detect if user starts an application, android

Thanks in advance for the help.
I have an app that can be started by either the user physically starting the app (like you would any normal app) or by a repeating service. Depending on what starts the app (the user or the service) I want to preform different initialization actions. How might I be able to detect if an user starts the app without doing anything custom (I imagine that there has to be some kind of built in setting in android for me to determine this)?
If service, that starts your Activity, is yours service, you can put some custom information (using Intent#putExtra for example) in Intent you use to start Activity from Service.
In Activity you can use Activity#getIntent(), that returns the intent that started this activity.
If you started Activity from Service, that Intent will be the one you passed in Service#startActivity, and will have your custom information. Otherwise, that was not your Service, that started your Activity.
That could look somehow like that, for example:
//in Activity
public static final String EXTRA_STARTED_FROM_MY_SERVICE = "com.example.extra_started_from_sevice";
private boolean wasActivityStartedFromService() {
Intent startingIntent = getIntent();
//assuming you will use Intent#putExtra in your service when starting activity
return startingIntent.getBooleanExtra(EXTRA_STARTED_FROM_MY_SERVICE, false);
}
//...
//in Service
//...
Intent startingIntent = new Intent(this, MainActivity.class);
startingIntent.putExtra(MainActivity.EXTRA_STARTED_FROM_MY_SERVICE, true);
startActivity(startingIntent);

Android how to open Activity when open home screen widget?

I am implementing an app related to widgets in this I prepared one widget and that's working perfectly and when click on views that opens the activity to prepare settings for that widget.
But my requirement is when click on home screen widget I want to open directly activity and then widget.
From the above picture when click on Custom Analog Clock widget open first Activity in my application and then I want to show the widget because from my activity I will setup some settings for that widget. How can I open activity first?
Widgets can have a special activity called when they are added to the home screen. That activity can return a SUCCESS intent with the widget ID receive in the "onCreate" method of the activity or RESULT_CANCELED if you want to prevent the creation of the widget itself.
You just need to add the configure activity class in a android:configure attribute at the appwidget-provider XML and add an intent filter with android.appwidget.action.APPWIDGET_CONFIGURE to your activity declaration at the AndroidManifest. It's detailed in http://developer.android.com/guide/topics/appwidgets/index.html#Configuring
The intent received has the widget ID:
public void onCreate(Bundle savedInstanceState) {
...
int widgetID = getIntent().getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
...
}
and then you can either finish your activity with setResult(RESULT_CANCELED); or confirm the widget creation with:
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetID);
setResult(RESULT_OK, resultValue);
You can do all your widget settings from that activity.
On the other hand, if you want to perform other setup that only needs to occur once for all your widgets instances, you can make then in the onEnabled method of your AppWidgetProvider class as described in http://developer.android.com/guide/topics/appwidgets/index.html#AppWidgetProvider
You should check out this tutorial: http://android-er.blogspot.nl/2010/10/simple-home-screen-app-widget-with.html It does exactly what you want.

How to Launch an Android AppWidget's configuration Activity from another activity?

Well this is driving me crazy. I have developed an App-widget. Everything is working fine.
I have a configuration activity which launches every time a widget is added on the home screen and works beautiful. I save the user settings per widget id etc.
The widget has some buttons, one of them launches an activity with about information, the "About Activity".
The "About Activity" has a button which I want to use to launch the configuration activity for the widget id that launched the "About Activity". The reason I want to do that is because I want the user to be able to configure the contents of any instance of my widget without having it removed and added again (in order to launch the configuration activity).
The configuration activity needs the AppWidgetManager.EXTRA_APPWIDGET_ID in order to make the job (save the user settings for this specific widgetid) so I must somehow pass this extra when I 'm calling it from another activity. The obvious think to do is this:
startActivity(new Intent(context,act_configure.class).putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, ??? ));
Now my question is where is the widgetid? I found a million ways to get the widgetids (the array) but not a single clue on how to get the specific widgetid which launched the "About Activity"
Any help about this will make the hours I spent to find a solution, worth something. Thank you in advance.
p.s. Please forgive my English as they are not my native language...
Thanks to Cory Chaltron here is the solution to my problem.
In the widget provider onUpdate method I should create a "unique" intent to pass to the pending intent which handles the launch of the about activity. Because of the way Android compares Intents, passing the WidgetID in the extras IS NOT ENOUGH, you should also pass it as data to the intent in order to be unique. So here is the code:
Intent aboutIntent = new Intent(cx, act_about.class);
aboutIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetIds[i]);
// Make this unique for this appWidgetId
aboutIntent.setData(Uri.withAppendedPath(Uri.parse("customuri://widget/id/"), String.valueOf(widgetID)));
PendingIntent aboutPendingIntent = PendingIntent.getActivity(cx, 0, aboutIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK), PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.cmdabout, aboutPendingIntent)
Although I answer my own question I am not accepting it because it is based on Cory's answer. Thank you all for the help...
How are you setting your widget views? I have an app where I iterate over the active widgets and configure set the RemoteView there. You could set your widget id in the onClick you are attaching to the "About" button.
final AppWidgetManager widgetManager = AppWidgetManager.getInstance(this);
final ComponentName widgetName = new ComponentName(this, WidgetProvider.class);
final int[] widgetIds = widgetManager.getAppWidgetIds(widgetName);
for (int widgetId : widgetIds) {
final RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.widget);
// This is the important part :-D
remoteViews.findViewById(R.id.your_about_button).setOnClickListener(... a listener to start your about activity that puts the widget id in the extra like you suggest in your question ...);
widgetManager.updateAppWidget(widgetId, remoteViews);
}

Transmit variables from service at onDestroy() to already opened Main-Activity

I have a Main-Activity which displays several spinners.
With a Toggle-Button in the Main-Activity I start a service which collects GPS-Data in background that measures the distance and sets some other variables.
When I stop the service with another click on the Toggle-Button in the Main-Activity, I stop the service, so the onDestroy() command is executed in the Service.
Within onDestroy in the Service, I want to submit the variables from the service to the already opened Main-Activity.
I tried that so far without success in the service:
Intent intent = new Intent(this, Main.class);
Bundle b = new Bundle();
Test = 2222;
b.putInt("Test", Test);
intent.putExtras(b);
I do not use the "startActivity(intent);", cause the activity that should the data send to is already open...the Main-Activity.
And on the side of the Main-Activity:
Bundle b = getIntent().getExtras();
if (b != null){
Test = b.getInt("Test");
}
else{
//..oops!
Toast.makeText(Main.this, "Oops...Nothing from service! :(", Toast.LENGTH_SHORT).show();
}
I never get data from the service.
I am trying this cause I need to start the service from the Main-Activity, from the Main activity the user stops the service and gets the values from it, then the Main-Activity should transmit the values from the service AND from the selected spinner values from itself to another activity.
I am trying that now for days and I also find some hints here, but nothing worked for me so far.
Someone any ideas?
Do I have to use a Broadcast-Receiver?
Whats the correct way to do this?
Before destroying the service store the data in shared preference and access it in the main activity
I don't know if this is the best way, but just have the Service you created update a static pojo. If the activity is already open then you could just set a timer to poll a the static pojo from the Activity to see if something has changed through an AsyncTask. When something has been updated in the pojo the AsyncTask can tell the Activity to update the UI accordingly. Then you don't have to play around with silly bundles. Also if your worried about your data getting lost you could always use SQLite. Hope this helps.
You need a broadcast receiver in main activity to get the data.

How to properly clear intent data from singleTop Activity?

Scenario is:
Activity with singleTop declared at the manifest. This activity is called with some extras. I'm able to clear the intent data and be able to flip screen and such without the original intent being called...
The issue is that when the activity is killed by the system and the user goes back to this activity it gets restarted using the original intent used to create the activity.
How do I remove this? I tried starting the same activity from within with a new Intent but no luck... I have also used various flags.
Well turns out that there isn't a proper way to clear intent data from the ActivityManager. The only way around this bug is to keep a flag state for when extras have been cleared. Please see the following post about keeping a flag.
https://groups.google.com/forum/#!topic/android-developers/vrLdM5mKeoY
I'm sending pending intents as alarms and I could receive differents alarms when my app is launched so the flag state doesn't suit me.
I did like this :
I'm sending intents with a id, for example intent.setAction("MyIntent"+System.getCurrentTimeinMillis);
storing this key as a reference in BD or shared prefs.
When you receive the intent on newIntent you ask the bd or shared prefs is the intent.getAction() is there and after that you clear the reference.

Categories

Resources