Run ContactsSyncAdapterService from outside Android app - java

If an app has an exported service in the android manifest that means I can run that service from within another app right? For example the service looks like this in the manifest file.
<service
android:name=".account.contactsync.ContactsSyncAdapterService"
android:exported="true"
android:process=":contacts" >
<intent-filter>
<action android:name="android.content.SyncAdapter" />
</intent-filter>
<meta-data
android:name="android.content.SyncAdapter"
android:resource="#xml/sync_contacts" />
<meta-data
android:name="android.provider.CONTACTS_STRUCTURE"
android:resource="#xml/contacts" />
</service>
As you can see the service is set to android:exported="true" which means I should be able to run it from outside of the app right? How do I do that
I tried this and it didn't work
final Intent intent = new Intent();
ComponentName cName = new ComponentName
("com.myapp","com.myapp.ContactsSyncAdapterService");
intent.setComponent(cName);
startActivity(intent);

You're missing an .account.contactsync in your component name:
ComponentName cName = new ComponentName("com.myapp",
"com.myapp.account.contactsync.ContactsSyncAdapterService");
And you'd need to use startService() instead of startActivity() - startActivity() only starts activities.

Related

No Activity found to handle Intent for Default Activity

I get error No Activity found to handle Intent when I try to start Default Activity from another Activity using
Intent start = new Intent("com.name.name.MainActivity");
startActivity(start);
finish();
I guess this is because in AndroidManifest for Default Activity I have android.intent.action.MAIN
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Is there a way to start Default Activity from another Activity when Default Activity is finished before?
You are trying to start the activity using the action name..
Note:startActivity() always adds CATEGORY_DEFAULT to an Intent if there is no other category specified(Here you have no category specified in your internt so its default).
Hence, an <intent-filter> for an <activity> always needs a <category>, whether DEFAULT or something else (here it needs default that's why error says No Activity found to handle Intent for Default Activity)
The LAUNCHER category says that this entry point should be listed in the application launcher.
The DEFAULT category is required for the Context.startActivity() method to resolve your activity when its component name is not explicitly specified.
Try to specify two intent filters:
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:windowSoftInputMode="stateHidden"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="com.name.name.MainActivity"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
Then you can start the activity using the action name:
Intent intent = new Intent("com.name.name.MainActivity");
startActivity(intent);
or simply you can go with the class name:
Intent intent = new Intent(context, MainActivity.class);
startActivity(intent);
Intent i = new Intent(otheractivity.this, MainActivity.class);
startActivity(i);

Android Manifest Confusion

I noticed that there are two ways to start an Activity with Intent.
1) Using - [android:name=".MainActivity"]
Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
2) Using - [action android:name="com.example.MainActivity"] from the Intent_Filter
String actionName = "com.example.MainActivity";
Intent intent = new Intent();
intent.setAction(CUSTOM_ACTION);
context.startActivity(i);
So what's the difference? Why do we have to set both the name and the intent_filter in the manifest if they both do the same thing?
===---==
Second confusion I have is... Is there a way to use the "OK Google" voice-launch option to launch an Activity that's NOT shown in the app list? Basically actual App Launcher launches the "default homepage" of the app, whereas voice launch takes you to a specific Activity directly?
I suspect some combo of these might get it done:
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
If your class is in your main package, you can use this way:
.YourClass
or
com.example.YourClass
If your class isn't in your main package, you should use this way:
packagename.YourClass

how to make our activity page open from another application?

I am new in android development, I want to build a music player but the problem in front of me is how do i make my app open from gallery.
For example,
if we want to open any music file then we select it, the the android mobile ask which player do we want to use.
so how can I add my app in that option.
please help.
If you want your app to be in the list for opening an audio file, you need to tell the system your app can open those files. You can do that by adding something like the following to your activity tag in your AndroidManifest.xml:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="content"/>
<data android:scheme="file"/>
<data android:mimeType="audio/*"/>
<data android:mimeType="application/ogg"/>
<data android:mimeType="application/x-ogg"/>
<data android:mimeType="application/itunes"/>
</intent-filter>
Intent filters are part of the core principles in Android development. You should try to get some knowledge on these basic topics before getting started.
It helps you
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(YOUR_SONG_URI);
intent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(intent);
Actually you can open activity not app at self just simply add action intent filter in mainfest like this
<activity class=".foo" android:label="#string/title_notes_list">
<intent-filter>
<action android:name="com.me.love" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
other app code :
Intent intent = new Intent("com.me.love");
startActivity(intent);
however if you want to tell android system that your activity can handle some actions like share or send data you just have to add "send" action to your activity in mainfest like so :
<activity class=".boo" android:label="#string/title_notes_list">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
so now when ever user click share button android system will check all activitys that have action string "android.intent.action.SEND" then give the user list of activitys that have send action and if you did added same action as boo activity dose to your activity then it will be one of chooses in the list.

Android app opening wrong page after using uri scheme

In IOS I have 2 apps (App A and App B) that will be performing different objective but at 1 point, App A will call App B using uri scheme to perform a function where App A does not have.
I wanted to implement this function to Android and currently have developed a demo for initiating call to App B. Now I am working on App B that will be receiving the uri scheme from the demo app.
Problem:
After App B return to the demo app using uri scheme I close the demo app.
When I go back to home page and open the multitask to reopen App B(or open App B through the icon), the page it reopen is the function that is used for demo app but what I wanted is the login page of App B.
This is a part of the Manifest for the intent-filter
<activity
android:name="com.apps.MasterActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:clearTaskOnLaunch="true"
android:windowSoftInputMode="adjustPan" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<data
android:host="x-callback-url"
android:scheme="myapp" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
This is where I call back to the demo app
Intent sendIntent = new Intent(Intent.ACTION_VIEW, responseUri);
sendIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(sendIntent);
Try adding this to your activity tag in AndroidManifest.xml
android:clearTaskOnLaunch="true"
Apparently I kinda found a solution that can force the app to show the login page after going back to the demo app. By putting this code in the activity.java file.
#SuppressWarnings("deprecation")
#Override
public void onDestroy()
{
super.onDestroy();
System.runFinalizersOnExit(true);
System.exit(0);
}
I know its a deprecated function that I'm using but will try to solve it when there's something wrong in the future or when I found a better solution for it.
Edit:
Found a better solution that does not need to use the deprecated function which I have forgotten to set it to false.
I have a flag that is set to true when the app is connected through Uri Scheme which i just have to set it to false and it will be fixed. This is the change location that I made
Intent sendIntent = new Intent(Intent.ACTION_VIEW, responseUri);
sendIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(sendIntent);
Util.isUriScheme = false;

DeviceAdmin add device intent from preference fragment xml

I'm trying to make an intent from a xml fragment :
<PreferenceScreen android:key="Admin" android:title="#string/prefTitleAdminApp" android:summary="#string/prefSumAdminApp">
<intent android:action="android.intent.action.ADD_DEVICE_ADMIN" />
</PreferenceScreen>
while having this in the manifest :
<receiver android:name="my.package.ARL.elements.AdminDevice"
android:label="ARL"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data android:name="android.app.device_admin" android:resource="#xml/admin" />
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
<action android:name="android.intent.action.ADD_DEVICE_ADMIN" />
</intent-filter>
</receiver>
When i click, i'm having a force close with the "No activity found to handle Intent { act=android.intent.action.ADD_DEVICE_ADMIN }
Where Am i wrong ? Is that possible (for device admin) ?
Is it possible to add the "extra" like this ? :
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,adminName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,getString(R.string.adminRights));
startActivityForResult(intent, 0);
But in xml format ? (didn't find the extra in the doc, there is only the data that look close)
Thanx
Edit: I found that it wasn't android.intent.action.ADD_DEVICE_ADMIN but DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN.
But it seems that calling such intent don't work from xml, this is still working :
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,"ARL");
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,getString(R.string.adminRights));
startActivityForResult(intent, 0);
If you have an idea on how to call such intent from xml, it would be great,

Categories

Resources