Is there any indication that my Android appwidget is "installed"? - java

I wrote an Android app with an appwidget.
I want to do this in my code:
if (widget_is_used_by_user)
Is there any way for me to know whether the user placed the widget on the home screen?

If You want to know if the Widget is installed, put som e Log, or whatever You want to do inside onEnabled():
public void onEnabled(Context context){
super(context);
Log.d("WIDGET","WIDGET ENABLED);
}
Also, sometimes it is required to set actions inside the receiver in manifest. I saw some posts about that, but in my widget I also get message without setting action:
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="android.appwidget.action.APPWIDGET_ENABLED" />
<action android:name="android.appwidget.action.APPWIDGET_DELETED" />
<action android:name="android.appwidget.action.APPWIDGET_DISABLED" />
</intent-filter>

If you don't want to keep track of them on your own like Opiatefuchs suggests, unfortunately there's no sure way.
You can query the AppWidgetManager to get a list of available appwidget IDs like this:
ComponentName widgetProvider = new ComponentName(this, "appwidget_class_name");
int[] appWidgetIds = AppWidgetManager.getInstance(this).getAppWidgetIds(widgetProvider);
for (int awId : appWidgetIds) {
// If there is any appwidget on the home screen, its ID should be in the list
}
I'm saying there's no sure way because the problem of phantom appwidget IDs (IDs created by the AppWidgetHost that have no "real" widget present on the home screen) is still a reality, even with recent Android versions.

Related

How to make the Android app load into logo then back to main activity?

I am new to App Development and I've been studying Kotlin for only a month now (1 hour everyday). I got the grasp of the functions but I still haven't gotten around using them for the purposes I have in mind.
Using Android Studio, I am trying to make the App load into a logo upon opening the app(Just like Facebook, Reddit), the logo is animated (which is not a problem for me). I have a couple of ways to achieve this but I wanna see what's the most efficient way to do it.
Create a new SplashActivity and change your starting activity in you manifest file like this:
<activity
android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
then in your SplashActivity's OnCreate, start your MainActivity
startActivity(Intent(this, MainActivity::class.java))
You can read more about this here.
You can create one Activity let's say SplashActivity which will have your logo and animations you want. Then from that Activity you can start your MainActivity. If you need to process some data inside your SplashActivity you do that then you start MainActivity. Otherwise, if you don't have any data to process you can simply start MainActivity with some delay, like this:
Handler().postDelayed({
val i = Intent(this, MainActivity::class.java)
startActivity(i)
}, 5000)
This will start your MainActivity after 5000 milliseconds which is 5 seconds. Now try to write some code and if you run into a problem try to find a solution or ask again if you can't. There is a lot of tutorials on the Internet you can find on how to do this.

Show app icon on contact details in Android

I'm trying to add my app icon in contact details, to work like a shortcut, i manage to show a title and subtitle but the app icon doesn't appear and i don't understand how to show him and how to add the event to navigate to an specific page after click in the custom cell.
someone can help me?
i have the following code on my contact manager:
public static void addContact(Context context, MyContact contact) {
ContentResolver resolver = context.getContentResolver();
// add condition that you want to check
ContentValues contentValues = new ContentValues();
contentValues.put(ContactsContract.Data.RAW_CONTACT_ID,
Integer.parseInt(contact.id));
contentValues.put(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE);
contentValues.put(ContactsContract.CommonDataKinds.Im.TYPE,
ContactsContract.CommonDataKinds.Im.TYPE_CUSTOM);
contentValues.put(ContactsContract.CommonDataKinds.Im.LABEL, "Title");
contentValues.put(ContactsContract.CommonDataKinds.Im.PROTOCOL,
ContactsContract.CommonDataKinds.Im.PROTOCOL_CUSTOM);
contentValues.put(ContactsContract.CommonDataKinds.Im.CUSTOM_PROTOCOL,
"Title");
contentValues.put(ContactsContract.CommonDataKinds.Im.DATA, "Transferência");
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(Data.MIMETYPE, MIMETYPE)
.withValues(contentValues).build());
try {
resolver.applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
String ex = e.getMessage();
}
}
`
and the following service on android manifest
<service
android:name="com.[appPackage].SyncService"
android:exported="true" >
<intent-filter>
<action android:name="android.content.SyncAdapter" />
</intent-filter>
<meta-data android:name="android.content.SyncAdapter" android:resource="#xml/syncadapter" />
<meta-data android:name="android.provider.CONTACTS_STRUCTURE" android:resource="#xml/contacts" />
</service>
someone can help me? Thx
First of all, each RawContact on the Contacts DB was synced by a particular app, so if that's not already the case for you, I would suggest you have your app insert its own RawContacts, and not mangle with RawContacts existing on the device, as they can be deleted, edited, reset by the controlling app at any time.
Now to get your app's "actions" displayed on the native contacts app, you need to declare your contacts custom mimetype and related fields, you do this with a special xml file called contacts.xml that you declare in your manifest which maps different DataKinds to Data columns like DATA1, DATA2, etc.
You then need to have an activity to catch the intent that will be fired when the user clicks on your custom row.
You declare the intent in the contacts.xml file as well, and you then need to set a similar intent-filter in your activity's manifest block so Android will know which activity to launch.
Here's official documentation: https://developer.android.com/guide/topics/providers/contacts-provider.html#CustomData & here: https://developer.android.com/guide/topics/providers/contacts-provider.html#SocialStreamDataKind
Here's a tutorial: https://medium.com/#stephen.cty/android-account-sync-adapter-and-contacts-contract-database-983281be4847
Again, try to make sure that the myRawContactId value in that tutorial is pointing to a RawContact your app had created, that will prevent future bugs with Google messing with your custom data rows

Android Studio Two Buttons to Two Pages

Good morning,
Quick coding inquiry I'd like to put forth.
I've gotten an assignment to produce an application on Android Studio that has two buttons that should each load new pages when clicked.
The chapter from our books explains how to do this with one button, but I assume there might be a different set up with two buttons?
Anyway, this is only the second assignment in our class so I'm not very far in understanding how all of this works; that and the class is an Independent Study course so I'm teaching myself. Not always the easiest thing.
Anyway, here's what I do have:
MainActivity.java
package com.example.thelatestmusicscene;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.buttonOne);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, MusicNewsOne.class));
}
});
Button button2 = (Button) findViewById(R.id.buttonTwo);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, MusicNewsTwo.class));
}
});
}
}
I believe this is the only file that's relevant to my problem but I can post the others if you need to see them too.
The first button functions perfectly but when I try to use the second button the app stops working. I've seen a few explanations online, but the few I've found about loading other pages gives me a bunch of errors in my code.
Note: This is how we were shown to do it from the book, but I'm all for alternative methods.
Everything else for this application is done, it's just this linking to the second java page I'm faltering on. If you know a better way to lay out the code I'm all ears or if you could link me to a resource that could teach me, I'd be grateful.
OK, this is going to be the confusing part. When I downloaded Android Studio for the class I could never get any of the emulators to run. This was a problem I also posted, which as of today, has still not received an answer. If you have a solution to that or want to see those details of what I’ve tried, go here:
https://superuser.com/questions/1394568/android-studio-and-haxm-installation
Ultimately, I could write code, but I couldn’t test it. However, I found a way to run the apps on my own smartphone to test if they’re functioning. Letting Android Studio build the APK file and then letting my phone run the app.
Now…as for errors. There are none. Android Studio isn’t coming back with any. As far as it's concerned, everything is fine. When I load the application on my phone and I click the second button, the application closes, and I get “Unfortunately, nameOfApp has stopped.”
And that’s where I find myself.
Here’s the XML (I assume you mean the AndroidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.thelatestmusicscene">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MusicNewsTwo">
</activity>
<activity android:name=".MusicNewsOne" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I've got to get some sleep now but I'll try retrieving a crash report from my phone today if I can.
Brother your MainActivity.java file have correct code.
Kindly check button name in xml and java file, and check your manifest file either MusicNewsTwo added as activity.
And be sure you have both java and xml file in correct place.
Few notes
You may need to provide the crash's log/stacktrace/message (for us to know what's the solution for it)
You may also need to provide us the layout or the .xml file
What could possibly went wrong
One of the common problems encountered:
Assuming you have only copied it from your class, you might have encountered the error saying that your MusicNewsTwo.class is not declared in the AndroidManifest.xml, do this:
What could be the possible solution
<application
android:label="Example APp"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name="package.path.to.MusicNewsOne">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Add this activity if missing -->
<activity
android:name="package.path.to.MusicNewsTwo"/>
</application>
I will update my answer so as long as you take into consideration the Few notes section of my answer, otherwise if I got your answer write already with this one, comment out.
Found the problem within my phone itself. Turns out it was a permission error for downloading 3rd party apps; my code was actually perfect. Thanks for all the suggestions though.
I think the IDs of the views in the layout : "activity_main" are different than the ones you are using in the MainActivity.java file
Bro there are two ways to do this, start a new Activity like you are doing, and other is By Using one activity you can post the fragments on the same activity as much as you want i.e 1 ,2 3 and so on.here is the link for fragments, but you are very much biggner ill suggest you to start with the actitvity and learn about the Activity life cycle. you can find here . moreover tell me what the error you are getting, To find the error see a the bottom of android studio the option (Logcat) click on it . and see the error and past it in the comment.

In android getting white screen before activity when first time app start on phone

Here is my source code of launcher activity.
I search a lot on stack overflow but nothing getting good answer
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_lang);
init();
}
private void init(){
spnLanguage = (Spinner)findViewById(R.id.spnLanguage);
btnNext = (Button)findViewById(R.id.btnNext);
btnNext.setOnClickListener(this);
}
Android apps do take some amount of time to start up, especially on a cold start. There is a delay there that you may not be able to avoid
https://www.bignerdranch.com/blog/splash-screens-the-right-way/
In the AndroidManifest file of your starting Activity, mention a transparent Theme
<activity
android:name="Your activity name"
android:theme="#android:style/Theme.Translucent.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
In my case delay was bit more. After searching a lot I found that it is because of android studio auto run feature. After disabling the auto run issue is solved.
I had a similar problem some time ago.
The activity is displayed to the user when it reaches the onStart() method. Your init() seems to take some time so until it finishes, you will only see the empty activity.
You could either trigger all heavy background work (that is not needed for the initial GUI setup) in a later method or first call another activity acting as splash screen if you have to keep all content of init() where it is.
I had also same issue please find below the question as well as the resolution:
White screen is displayed first time on gradle build.

Android receive images from Gallery in order

I currently have an Android application that has an intent-filter to receive images from the Gallery. It is important that the images are received in the same order that the user selected them in. This seems to be the default behavior on most devices, however on some devices (so far I've seen this on Motorola's running Android 4.x) the order seems undefined. Does anyone know a way to declare in the intent that the images should be received in order? Or a way once the images are recieved to determine the selected order?
Here is relevant code from the manifest
<activity
android:label="#string/app_name"
android:name=".activities.ImportImagesActivity" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
</activity>
And from ImportImagesActivity
private List<Uri> parseIncomingData() {
List<Uri> uriList = null;
Intent intent = this.getIntent();
if(intent != null) {
String action = intent.getAction();
//Single Image
if (action.equalsIgnoreCase("android.intent.action.SEND")) {
//removed for brevity
}
}
//Multiple Images
else if (action.equalsIgnoreCase("android.intent.action.SEND_MULTIPLE")) {
uriList = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
}
}
//more code - at this point images have been recieved
return uriList;
}
EDIT
To give a little context, let me explain the general flow of the app.
The user opens the Gallery and selects images. They choose to 'Share' them with my app. My application receives a list of Uri's which are then displayed using an internal gallery backed by a custom Adapter class. The images display correctly based on the Uri list, the issue is the order of the List<Uri> is sometimes incorrect. It is important to my users that the images appear in the same order they select them in.
Clarification
When I use the term Gallery I am referring to the built in Android app Gallery.
When I use the term 'Share' I am referring to the the Share button within the Gallery app. This allows the user to select from a list of services such as Facebook, Email, and in this case my app.
For Example
imagine a Gallery with 3 images, displayed in an arbitrary order: A, B, and C. The user selects first C then A then B and chooses to share them with my app. On most phones my list will be correctly ordered {C, A, B}, on offending phones this order seems random.
I cannot use the creation timestamp because the creation time is generally irrelevant to the selection order. Adding custom meta data doesn't help either because I don't know the correct initial order.
My observation is that Android gallery displays images in accordance to their recency.
For the devices where you're unable to determine the order, you can import the images from the gallery and check their creation time. Here's a way to do that. Or you could use a metadata extractor app, many jars can be found.
Now, you could just arrange the images in the order of recency and you should be done.
[EDIT]
I have a question. You said they may be selected in any order, so are they "uploading" it onto a server by "sharing"?
If so, then one way is to check which image was uploaded or if you want the order of selection, you could do this. Edit the metadata of the images, there's bound to be a useless tag, select one and edit it on touch. So, if I select image A it changes to 1 and then I select image B it becomes 2. But if I unselect image A now then image B should become 1. So, you could use nodes here. This is the first in first out (FIFO) method. Upon un-selection, A is thrown out of the list and B replaces it.
Is this what you wanted?
EDIT
Sorry, I don't think you can do this without creating your own gallery. Why don't you just import the android gallery into a grid view in your app?
Yeah, I just faced the same problem right now. I am also using Fedor's lazy loading concept.I am not sure how far this will be helpful and whether this is the right approach. But still it solved the problem for me.
I had to do a little modification in the getView(),
#Override
public View getView(int position, View convertView, ViewGroup parent) {
System.gc();
ImageView view;
if(convertView == null) {
view = new ImageView(context);
view.setScaleType(ImageView.ScaleType.FIT_CENTER);
view.setLayoutParams(new GridView.LayoutParams(screenWidth/4, screenHeight/4));
view.setAdjustViewBounds(false);
view.setPadding(2, 2, 2, 2);
System.gc();
}else {
view = (ImageView) convertView;
}
if(view!=null)
{
imageLoader.DisplayImage(urlList.get(position), view);
notifyDataSetChanged(); //Calling this helped to solve the problem.
}
System.gc();
return view;
}

Categories