How to host widgets and how to update them always reliably? - java

I'm hosting widgets in my app. They can be picked from a list of all installed widgets and are added to a view. I save their IDs to database and restore them from these saved IDs again after restart of the app.
This works most of the times, but not always. Widgets are not always updated properly. One scenario where that happens for sure is:
"rebooting phone and starting app before boot-process of device has finished."
(if i wait until booting is completed, it works!!!)
There must be another scenario, but i couldnt reproduce that. I restart my app and they are not updated. So i wonder if i am missing something important.
I have basically used this tutorial:
http://leonardofischer.com/hosting-android-widgets-my-appwidgethost-tutorial/#comment-14678
My Code:
onCreate:
mAppWidgetManager = AppWidgetManager.getInstance(this);
mAWH_AppWidgetHost = new MyAppWidgetHost(MyApp.getContext(), R.string.APPWIDGET_HOST_ID);
onStart:
mAWH_AppWidgetHost.startListening();
onStop:
mAWH_AppWidgetHost.stopListening();
restore from id (saved in database):
AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
MyAppWidgetHostView hostView = (MyAppWidgetHostView) mAWH_AppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
hostView.setAppWidget(appWidgetId, appWidgetInfo);
mLL_innerLayout.addView((MyAppWidgetHostView) hostView);
I cannot find any sites in the web that make this clear. Neither does looking at source code of different launcher apps make it more clear to me.

I had the same problem, but then I moved
mAWH_AppWidgetHost.startListening();
in onCreate method and
mAWH_AppWidgetHost.stopListening();
in onDestroy method. This is suggested here

Related

Share data between two app using SharedPreferences

I am sharing data between two my app, this is my code for get the data from the shared pref in app A
try {
final Context mContext = createPackageContext("com.example.demo", Context.MODE_PRIVATE);
final String val = mContext.getSharedPreferences("pref_name",Context.MODE_PRIVATE).getString(MY_KEY,"");
Log.e("sharedtest",val);
finish();
} catch (Exception e) {
e.printStackTrace();
}
this code is inside the onCreate() method, I don't have any more code anywhere. My problem is that, if I save the some value in my app B and than start my app A the saved data were correctly retrieved at first time, after retrieving the data my activity were finishing (I have only one activity), and if I start my launcher icon and start my app A, there is no updated data(it is the same), which where changed from app B.
also if I kill my app from system app settings and launch it like first time launch updated data is here, every data change needs my app killing from settings, how can I fix that? what I'm missing?
I found solution it may be a trick but it works fine for me,
after my app A finishes its job, I'm calling the system exit method.
System.exit(1);
it makes app "A" to be exit and finish job completely
After that I had the latest updated data in my preferences

How to get 'correct' default launcher programmatically? Shortcuts lose their title

What have I done so far:
I am currenctly facing some problems with the launchers.
My application adds shortcuts to the workspace of the launcher (homescreen).
But on some devices (Samsum Duos) for example, the titles and /or icons
are changed back after reboot to my default application one.
So I am currently going through 1000s of lines code in the android
source to identify the problem, but was not able to find it.
I saw in InstallShortcutReceiver
a comment in line 183 that the "name" provided by Intent.EXTRA_SHORTCUT_NAME can in
some situations be used only for comparison etc and will be replaced
with the applications default name.
// This name is only used for comparisons and notifications, so fall back to activity name
// if not supplied
But (my Samsum Duos is rooted) I could find the complete information's
about the cell position and shortcutInfo's inside of the launcher.db.
So it was not gone, after reboot, but maybe only not correct initialized!
First Question:
Does anybody know the reason for a custom, programmatically created shortcut to change the title and or icon back to the application's one that created it?
Next story:
I noticed that this issue was reproducible on my Samsum Duos, so I decided
to exclude the Devices Launcher from my "save launcher" list.
To receive the default launcher I am doing the following:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
ResolveInfo resolveInfo = null;
try {
resolveInfo = context.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY); //can return null!
}catch(RuntimeException e){
ExceptionHandler.logAndSendException(e);//package manager has died
return false;
}
But the problem now is, that it always returns that the default launchers
package is: com.android.launcher2.Launcher, which would be the standard
android stock launcher. But I know that Samsum uses the TouchWiz home
launcher, whos Package is located under com.sec.android.app.launcher!
That is also where I found the launcher.db and all its ShortcutInfo's.
2. Second Question
How do I get reproducible the correct default launcher package to identify
which launcher is used?
edit:
I kind of fixed the second problem. Somehow the ResolveInfo I get from
the PackageManager seems to be not reliable.
For the Samsum Duos I get:
resolveInfo.activityInfo.name = com.android.launcher2.Launcher
resolveInfo.activityInfo.packageName = com.sec.android.app.launcher //this is what I need
But for the Redmi MIUI:
resolveInfo.activityInfo.name = com.miui.home.launcher.Launcher //this time I would need this
resolveInfo.activityInfo.packageName = com.miui.home //the packageName is not complete!
I need an unique identifier for the launcher! So I thought activityInfo.name would be the
way to go, but it isn't in some situations. And the packageManager seems to apply to too many devices. Any suggestions?
Cheers!

Eclipse plug-in: bind data to a list and refresh view

I am working on an Eclipse plugin-project where I have multiple services which have to register themselves to the master-project (stored in a list). I have one view where I want to list each service (that successfully registered itself) and its status (if enabled/disabled -> from settings) which should be updated if the status changes.
What i tried:
The adding works as expected and the list in the master-project knows the single services. Now I have problems to show the details (service + it's status). I tried to add a new label for each service (with a for-each-loop). Basically this works, but I don't know how to refresh the view after I get the event (in the view) that a service was added. Moreover I don't know how to bind the propertychanged-event to such a label. (I know how to do it if the labels are hard coded, but that's not possible here).
How can I achieve what I want? (show label & status for each registered service and refresh status if it changes)
Thank you!!
For the sake of completeness, I add my answer/solution I found here:
Eclipse plug-in: bind data to a list and refresh view

Android fragment re-use during re-orientation of screen

I'm struggling a bit with my use of fragments for ICS. I've got a fragment (call it AnalysisFragment) that does a fair amount of number-crunching work, reading in a file and then performing a number of calculations on it. Most of this work is initiated in onActivityCreated, because I need to consult the app's settings file prior to doing the work, which in turn requires having the context, which is accessible through the activity, which isn't necessarily available until onActivityCreated is invoked.
Well, this work seems to be getting done again (twice!) whenever I rotate the display. In my LogCat, I see onDestroyView and onDestroy happening (once), followed by onCreate, onCreateView, and onActivityCreated (twice), all pertaining to AnalysisFragment.
Obviously, the work of re-painting the screen for a different orientation has to be done over again, but is there any way I can add avoid the number-crunching work a 2nd time (not to mention a 3rd time)?
I maybe should add that I'm instantiating a new copy of AnalysisFragment whenever the user selects a new file to analyze, as follows
public static AnalysisFragment newInstance
(
String ndsFileName,
ViewIndicator viewIndicator
)
{
AnalysisFragment analysisFragment = new AnalysisFragment( );
Bundle bundle = new Bundle( );
bundle.putString( "nds_file_name", ndsFileName );
bundle.putString( "view_indicator", viewIndicator.toString( ) );
analysisFragment.setArguments( bundle );
return analysisFragment;
}
However, during a re-orientation, Android must be doing it for me. After shutting down the instance I created, it instantiates another using the same bundle for the new screen orientation. Eventually, onActivityCreated gets called on the new instance, and my code is off reading in the same file it read moments ago and re-doing the number crunching.
Any insight would be appreciated ...

Why do I get "Unable to start service Intent"?

I'm trying get some licensing code from AndroidPit.com working, but I get "Unable to start service Intent". Basically my code looks like this:
Intent licenseIntent = new Intent("de.androidpit.app.services.ILicenseService");
if (mContext.bindService(licenseIntent, this, Context.BIND_AUTO_CREATE))
{
// success
}
else
{
// failure (I get this all the time(
}
I tried passing ILicenseService class explicitly:
Intent licenseIntent = new Intent(mContext, de.androidpit.app.services.ILicenseService.class);
but I still get the same problem.
I managed to get Android Market LVL library working which uses identical code, so I don't really understand why it fails to find "de.androidpit.app.services.ILicenseService", but manages to find "com.android.vending.licensing.ILicensingService".
Most of the answers I found here say that you need to append stuff to your AndroidManifest.xml, but you don't anything for "com.android.vending.licensing.ILicensingService" to work, so I guess I shouldn't need anything "de.androidpit.app.services.ILicenseService" (they both derive from android.os.IInterface).
Thanks in advance.
Most of the answers I found here say that you need to append stuff to your AndroidManifest.xml
Those answers are correct.
but you don't anything for "com.android.vending.licensing.ILicensingService" to work
That is because com.android.vending.licensing.ILicensingService is a remote service, one that is not in your project, but rather in the firmware of the device.
so I guess I shouldn't need anything "de.androidpit.app.services.ILicenseService" (they both derive from android.os.IInterface).
That is flawed reasoning. By your argument, java.util.HashMap does not go in the manifest, and both java.util.HashMap and any implementation of Activity all derive from Object, so therefore you do not need to put your activities in the manifest. If you try this, you will quickly discover that your activities no longer work.
If it is a component (activity, service, content provider, or some implementations of BroadcastReceiver), and the implementation of the component is in your project (directly, via a JAR, via a library project, etc.), you must have an entry in the manifest for it.
Wherever you got the service from should provide you with instructions for adding the service to your manifest, and they should also supply you with instructions for creating the Intent used to bind to it. If they do not provide this documentation, perhaps you should reconsider your use of this product.
The solution in my case was to start a server part on my phone (AppCenter from AndroidPit.com in this case). No entries in AndroidManifest are necessary for the client application.

Categories

Resources