Unity3D Android plugin,: Unable to start service, intent error - java

I am trying to start a service inside a Unity3d Android plugin but can not make it work. Is working on Android Studio test app, but the service call fail inside unity.
My service class
public class ProximityService extends Service {
private String TAG = "iProximity: ";
NotificationManager _NotificationManager;
private Context _Context;
private static Timer _Timer = new Timer();
public ProximityService() {
}
private void sendNotification() {
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder;
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, iRealUnityPlayerActivity.class), 0);
builder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ireal)
.setContentTitle("Notification")
.setContentText("message")
.setTicker("msg: mensaje")
.setSound(sound)
.setAutoCancel(true);
builder.setContentIntent(contentIntent);
_NotificationManager.notify(0, builder.build());
}
#Override
public void onCreate() {
Log.i(TAG, "onCreate()");
super.onCreate();
_Context = getApplicationContext();
_NotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
// methods
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommanf()");
//startProximity();
sendNotification();
return START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onBind()");
// TODO: Return the communication channel to the service.
return null;
}
}
Java class that is used to start the service:
public final class StatusCheckStarter {
static Context myContext;
// Called From C# to get the Context Instance
public static void ReceiveContextInstance(Context tempContext) {
myContext = tempContext;
}
public static String StartProximityService()
{
String result = "OK";
try
{
myContext.startService(new Intent(myContext, ProximityService.class));
}
catch (Exception e) {
e.printStackTrace();
result = e.getMessage();
}
return result;
}
public static String Dummy() {
return "DONEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE";
}
}
Unity C# code that is used to call the java functions to start the service:
AndroidJavaClass unityClass;
AndroidJavaObject unityActivity;
AndroidJavaObject unityContext;
AndroidJavaClass customClass;
string a1 = "";
string a2 = "";
string a3 = "";
string a4 = "";
void Start () {
//Replace with your full package name
sendActivityReference("info.ireal.proximitylib.StatusCheckStarter");
//Now, start service
startService();
Debug.Log ("START");
}
void sendActivityReference(string packageName)
{
unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
unityActivity = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
unityContext = unityActivity.Call<AndroidJavaObject>("getApplicationContext");
customClass = new AndroidJavaClass(packageName);
customClass.CallStatic("ReceiveContextInstance", unityContext);
}
void startService()
{
a4 = customClass.CallStatic<string> ("Dummy");
a3 = customClass.CallStatic<string>("StartProximityService");
}
The Dummy method is working and return a string value, but the service does not work
Adb logcat message:
Unable to start service Intent {
cmp=info.ireal.proximitytest/info.ireal.proximitylib.ProximityService
VirtualScreenParam=Params{mDisplayId=-1, null, mFlags=0x00000000)} }
U=0: not found
I really appreciate any help Best regards
Mariano
I am using the solution from this thread but still cant make it work.
EDIT
My AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.unity3d.player"
android:installLocation="preferExternal"
android:versionCode="1"
android:versionName="1.0">
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true"/>
<application
android:theme="#style/UnityThemeSelector"
android:icon="#drawable/app_icon"
android:label="#string/app_name"
android:debuggable="true">
<activity android:name="com.unity3d.player.UnityPlayerActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
<service android:name="info.ireal.proximitylib.ProximityService" />
<!---<service android:name="ProximityService" />-->
</application>
</manifest>

This could be the Manifest. Make sure to include the service class in the service tag.
It could be any of these problems below. Build, Run and test each one you try.
1.Add the class to the Manifest.
<application
<service android:name=".ProximityService" />
</application>
2.Add the class with the full path to the Manifest.
If this does not work, use the full service class package name
<application
<service android:name="info.ireal.proximitylib.ProximityService" />
</application>
3.Add the class to the Manifest from Unity itself.
Again, if that fails, go to <UnityInstallationDirecory>\Editor\Data\PlaybackEngines\AndroidPlayer\Apk
Copy the AndroidManifest.xml from that place file to your Assets\Plugins\Android directory then include the service tag code above in the Manifest. Save and run.
4.Finally, use Activity instead of Context.
Changes on the Java side:
A.Rename static Context myContext; to static Activity myActivity;
B.Change
public static void ReceiveContextInstance(Context tempContext) {
myContext = tempContext;
}
to
public static void ReceiveContextInstance(Activity tempActivity) {
myActivity = tempActivity;
}
Changes on C# side:
C.Replace customClass.CallStatic("ReceiveContextInstance", unityContext); with customClass.CallStatic("ReceiveContextInstance", unityActivity);

Related

App crashes after granting USB permission - Unity app using aar java plugin

I am creating an Unity app which will be controlling Numato GPIO USB powered controller through smartphone USB connection. Since I have to connect the controller to phone I have no debug log so I have no idea what is going on. Thus, I include plugin code and a custom manifest which I use in Unity.
I get questioned by the App if I want to grant permission to control the device (Shows right device name etc) and after I grant the permission app crashes immediately.
Is there a way to check what causes the error? Or maybe I don't see something obvious here.
public class PluginInstance extends Activity {
private static Activity unityActivity;
private static Context unityContext;
public Gpio laser1;
public static void receiveUnityActivity(Activity tActivity) {
unityActivity = tActivity;
}
public static void receiveUnityContext(Context tContext) { unityContext = tContext; }
//Debugging purposes
public void Toast(String msg) {
Toast.makeText(unityActivity, msg, Toast.LENGTH_SHORT).show();
}
//Action Usb Permission
public static final String ACTION_USB_PERMISSION = "com.unity3d.player.UnityPlayerActivity.USB_PERMISSION";
public DevicesManager mDevicesManager;
//USB permission
public final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
UsbDevice device = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if (device != null) {
Bundle deviceIndexBundle = intent.getExtras();
if (deviceIndexBundle == null) {
return;
}
int deviceIndex = deviceIndexBundle.getInt(AppConstant.EXTRA_DEVICE_INDEX);
Toast.makeText(unityActivity, "Usb permission granted", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(unityActivity, "Usb permission declined", Toast.LENGTH_SHORT).show();
}
unregisterReceiver(mUsbReceiver);
}
}
}
};
public void InstantiateManager() {
mDevicesManager = DevicesManager.getInstance();
}
public void GetGpios() {
NumatoUSBDevice numatoUSBDevice = mDevicesManager.getDevices().get(0);
Gpio lazer1 = numatoUSBDevice.mGpios.get(1);
}
public void GpioOn() {
laser1.setCurrentOutputState(true);
laser1.setState(true);
}
public void GpioOff() {
laser1.setCurrentOutputState(false);
laser1.setState(false);
}
public void EnumerateDevices() {
UsbManager manager = (UsbManager) unityActivity.getSystemService(Context.USB_SERVICE);
int index = 0;
ArrayList<UsbDevice> cdcAcmDevices = CdcAcmDriver.ListDevices(manager);
mDevicesManager.clearDevices();
ArrayList<Integer> supportedDevices = NumatoUSBDevice.GetSupportedProductIds();
if(!cdcAcmDevices.isEmpty()){
for (UsbDevice cdcAcmDevice : cdcAcmDevices){
int vendorId = cdcAcmDevice.getVendorId();
if(vendorId == NumatoUSBDevice.VID_NUMATOLAB && supportedDevices.contains(cdcAcmDevice.getProductId())){
mDevicesManager.addDevice(new NumatoUSBDevice(index, cdcAcmDevice, manager));
index++;
}
}
}
}
public void MakeConnection() {
NumatoUSBDevice numatoUSBDevice = mDevicesManager.getDevices().get(0);
UsbManager manager = (UsbManager) unityActivity.getSystemService(Context.USB_SERVICE);
//TODO unityContext in mPermissionIntent
PendingIntent mPermissionIntent = PendingIntent.getBroadcast(unityContext, 0,
new Intent(ACTION_USB_PERMISSION).putExtra(AppConstant.EXTRA_DEVICE_INDEX, 0), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
unityActivity.registerReceiver(mUsbReceiver, filter);
manager.requestPermission(numatoUSBDevice.getDevice(), mPermissionIntent);
}
}
And the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<!-- GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN-->
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.unity3d.player"
xmlns:tools="http://schemas.android.com/tools">
<application>
<activity android:name="com.unity3d.player.UnityPlayerActivity"
android:theme="#style/UnityThemeSelector">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"/>
</intent-filter>
<meta-data
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="#xml/device_filter"/>
</activity>
</application>
</manifest>
Edit: I managed to access debug log through wifi and this is what I get:
FATAL EXCEPTION: main
Process: com.jonquil.A2NumatoController, PID: 16837
java.lang.RuntimeException: Error receiving broadcast Intent { act=com.unity3d.player.UnityPlayerActivity.USB_PERMISSION flg=0x10 (has extras) } in com.jonquil.unityplugin.PluginInstance$1#7d45921
at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1689)
at android.app.LoadedApk$ReceiverDispatcher$Args$$ExternalSyntheticLambda0.run(Unknown Source:2)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7838)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.Context.unregisterReceiver(android.content.BroadcastReceiver)' on a null object reference
at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:769)
at com.jonquil.unityplugin.PluginInstance$1.onReceive(PluginInstance.java:63)
at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1679)
... 9 more
This looks like there is a problem with broadcast receiver since error is caused by invoking method on a null object reference.

Unable to get intent from service with LocalBroadcastManager [duplicate]

I have my main activity that start a service (Location service) and I want that service to broadcast the new location each time a new location is found.
Thanks to the log I know the service is working and I have new locations every seconds or so, but I never get the broadcast.
MainActivity.java
public class MainActivity extends Activity {
private static final String TAG = "mainActivity";
private CMBroadcastReceiver mMessageReceiver = new CMBroadcastReceiver();
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
// Start Service
startService(new Intent(this, LocationService.class));
super.onCreate(savedInstanceState);
}
#Override
public void onResume()
{
LocalBroadcastManager.getInstance(this).registerReceiver(
mMessageReceiver, new IntentFilter(CMBroadcastReceiver.RECEIVE_LOCATION_UPDATE));
super.onResume();
}
#Override
public void onPause()
{
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onPause();
}
}
CMBroadcastReceiver.java
public class CMBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "CMBroadcastReceiver";
public static final String RECEIVE_LOCATION_UPDATE = "LOCATION_UPDATES";
#Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Received broadcast");
String action = intent.getAction();
if (action.equals(RECEIVE_LOCATION_UPDATE))
{
Log.i(TAG, "Received location update from service!");
}
}
}
LocationService.java
/**
* Callback that fires when the location changes.
*/
#Override
public void onLocationChanged(Location location) {
mCurrentLocation = location;
mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
Log.i(TAG, "onLocationChanged " + location);
Intent intent = new Intent(CMBroadcastReceiver.RECEIVE_LOCATION_UPDATE);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
Log.i(TAG, "Broadcast sent");
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cyclemapapp.gpstracker">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
android:theme="#style/AppTheme.NoActionBar">
android:configChanges="orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".LocationService" android:process=":location_service" />
</application>
I the log I can see that "Broadcast Sent" But I never get the "Broadcast Received"
Any help will would be greatly appreciated.
EDIT:
Edited how the intent was created in the location service as Shaishav suggested.
Still doesn't work.
LocalBroadcastManager does not work across processes. Your Service is running in a separate process.
You can either run your Service in the same process as the Activity - by removing the process attribute from the <service> element - or use some sort of IPC instead - e.g., by sending and receiving the broadcasts on a Context instead of LocalBroadcastManager.
In your LocationService, send local broadcast using:
Intent intent = new Intent(CMBroadcastReceiver.RECEIVE_LOCATION_UPDATE);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
<service android:name=".LocationService" android:process=":location_service" />
Your service is in a separate process from the activity. LocalBroadcastManager is only for use in one process. Either remove android:process from the <service>, or use some IPC mechanism (e.g., system broadcasts, properly secured).

How to stop JobService Scheduled to when app is removed from background?

Currently i am working with an application and my app has a feature that the user will be able to click on a Navigate button and my app will start the Google Map. Till now it's fine and i have done it. But the fact where i am stuck is that i want my app to perform some tasks. To achieve that i have used JobService and scheduled it to run after every 5 seconds even when the app is in background.
When the user presses the back button then inside onDestroy method i have cancelled the scheduler. But when the app is removed from the background by sliding or pressing the cross icon the JobService keeps running as the onDestroy method can be called or not by the os when it is removed from the background. How can i stop the scheduled job when the app is removed from the background?
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="javarank.com.serviceinbackground">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyJobService" android:exported="true" android:permission="android.permission.BIND_JOB_SERVICE" />
</application>
</manifest>
MyJobService class
public class MyJobService extends JobService {
#Override
public boolean onStartJob(final JobParameters jobParameters) {
Toast.makeText(getApplicationContext(), "Doing job", Toast.LENGTH_SHORT).show();
jobFinished(jobParameters, true);
return false;
}
#Override
public boolean onStopJob(JobParameters jobParameters) {
return false;
}
}
Here is my MainActivity
public class MainActivity extends AppCompatActivity {
private static final int JOB_ID = 1;
private JobInfo jobInfo;
private JobScheduler scheduler;
private Button navigateButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ComponentName componentName = new ComponentName(this, MyJobService.class);
JobInfo.Builder builder = new JobInfo.Builder(JOB_ID, componentName);
builder.setPeriodic(5000);
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
// if true this job exists even after a system reboot...
builder.setPersisted(false);
jobInfo = builder.build();
scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
scheduler.schedule(jobInfo);
navigateButton = (Button) findViewById(R.id.navigate_button);
navigateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
StringBuffer url = new StringBuffer("https://www.google.com/maps/dir/?api=1");
url.append("&origin=23.755736,90.374627");
url.append("&destination=23.754047,90.371682");
url.append("&travelmode=driving");
Uri gmmIntentUri = Uri.parse(url.toString());
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
}
});
}
#Override
protected void onDestroy() {
Toast.makeText(getApplicationContext(), "Destroy called.", Toast.LENGTH_SHORT).show();
scheduler.cancel(JOB_ID);
super.onDestroy();
}
}
I think you need to override following onStop() method and put stopService() command to stop the JobService.
#Override
protected void onStop() {
// A service can be "started" and/or "bound". In this case, it's "started" by this Activity
// and "bound" to the JobScheduler (also called "Scheduled" by the JobScheduler). This call
// to stopService() won't prevent scheduled jobs to be processed. However, failing
// to call stopService() would keep it alive indefinitely.
stopService(new Intent(this, MyJobService.class));
super.onStop();
}
You can create a new service like
MyService.java
public class MyService extends Service {
public MyService() {
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
//stop you jobservice from here
stopSelf();
}
}
and start it from MainActivity.java
startService(new Intent(MainActivity.this,MyService.class));
Android> 7 automatically saves battery power. You must turn on the application's battery saving stop feature.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent intent = new Intent();
String packageName = getPackageName();
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
if (!pm.isIgnoringBatteryOptimizations(packageName)) {
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
startActivity(intent);
}
}
add this to AndroidManifest.xml
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
I faced this issue, but I found that after schedule job service, it can't be canceled (From view).
So I turned to stop it inside the job service by calling onStopJob(params)
and it worked.

addAccount not called from AbstractAccountAuthenticator implementation from MainActivity

I am following a tutorial for adding a user account to the Android AccountManager.
In my main activity, I have the following method:
private void addNewAccount(String accountType, String authTokenType) {
Log.d(TAG,"addNewAccount called");
final AccountManagerFuture<Bundle> future = mAccountManager.addAccount(accountType, authTokenType, null, null, this, new AccountManagerCallback<Bundle>() {
#Override
public void run(AccountManagerFuture<Bundle> future) {
try {
Bundle bnd = future.getResult();
Log.d("ACME", "AddNewAccount Bundle is " + bnd);
} catch (Exception e) {
e.printStackTrace();
}
}
}, null);
}
This method is being called, as I see the log in the logcat. Now my AbstractAccountAuthenticator implementation is as follows:
public class AcmeAuthenticator extends AbstractAccountAuthenticator {
private String TAG = "AcmeAuthenticator";
private final Context mContext;
public AcmeAuthenticator(Context context) {
super(context);
this.mContext = context;
}
#Override
public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options) throws NetworkErrorException {
Log.d("acme", TAG + "> addAccount");
final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
intent.putExtra(AuthenticatorActivity.ARG_ACCOUNT_TYPE, accountType);
intent.putExtra(AuthenticatorActivity.ARG_AUTH_TYPE, authTokenType);
intent.putExtra(AuthenticatorActivity.ARG_IS_ADDING_NEW_ACCOUNT, true);
intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
final Bundle bundle = new Bundle();
bundle.putParcelable(AccountManager.KEY_INTENT, intent);
return bundle;
}
The above method is never called. The following is the service I have created for it:
public class AcmeAuthenticatorService extends Service {
#Override
public IBinder onBind(Intent intent) {
AcmeAuthenticator authenticator = new AcmeAuthenticator(this);
return authenticator.getIBinder();
}
}
And my manifest definition is as follows:
<activity android:name="com.exercise.accountmanagerstudy.accountAuthenticator.AuthenticatorActivity" android:label="#string/login_label"/>
<service android:name=".accountAuthenticator.AcmeAuthenticatorService">
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator" />
</intent-filter>
<meta-data android:name="android.accounts.AccountAuthenticator"
android:resource="#xml/authenticator" />
</service>
<!-- client -->
<uses-permission android:name="android.permission.USE_CREDENTIALS"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS"/>
<!-- Authenticator -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS"/>
I am not getting an compiler errors, the addAccount override in the AbstractAccountAuthenticator implementation is not called. from the main activity addNewAccount method. I have researched around for a few links here and here.
Any help will be appreciated.
Ok, so I finally did figure it out. Apparently, the authenticator.xml file for AcmeAuthenticator has a field called accountType:
<?xml version="1.0" encoding="utf-8"?>
<account-authenticator
xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="com.exercise.accountmanagerstudy"
android:icon="#drawable/ic_launcher"
android:smallIcon="#drawable/ic_launcher"
android:label="#string/label"
android:accountPreferences="#xml/prefs"/>
When I was calling addNewAccount in my main activity, I was supposed to pass the exact value of the accountType in the above mentioned xml as the accountType argument. Phew, that took me quite sometime and hope it helps someone else :-).

Android bindService() returns false when using AIDL?

For some reason I cannot bind to my service? I need the onServiceConnected() method to run so that I can use my AIDL interface. What am I doing wrong?
private ServiceConnection serviceConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName className, IBinder service) {
Log.d("BindingService", "Service trying to bind!");
sendService = ISendMessageService.Stub.asInterface((IBinder) service);
boundToService = true;
pendingFragment.bindToService();
}
#Override
public void onServiceDisconnected(ComponentName arg0) {
sendService = null;
boundToService = false;
pendingFragment.unbindService();
}
};
The main activity methods:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
schedulerFragment = new SchedulerFragment();
pendingFragment = new PendingFragment();
fm = getFragmentManager();
titleBar = getActionBar();
titleBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
scheduleTab = titleBar.newTab();
pendingTab = titleBar.newTab();
scheduleTab.setText("Schedule");
pendingTab.setText("Pending");
scheduleTab.setTabListener(new MyTabListener(schedulerFragment));
pendingTab.setTabListener(new MyTabListener(pendingFragment));
titleBar.addTab(scheduleTab);
titleBar.addTab(pendingTab);
titleBar.selectTab(scheduleTab);
Boolean success = getApplicationContext().bindService(new Intent(SendMessageService.class.getName()),
serviceConnection, Context.BIND_AUTO_CREATE);
Log.d("ServiceConnection", success.toString());
}
#Override
public void onStop() {
super.onStop();
if (boundToService) {
unbindService(serviceConnection);
}
}
#Override
public void onPause() {
super.onPause();
if (boundToService) {
unbindService(serviceConnection);
}
}
This is the service's onBind() function:
#Override
public IBinder onBind(Intent intent) {
mBinder = new ISendMessageService.Stub(){
#Override
public void deleteMessage(int index) throws RemoteException {
TimedMessage m = schedule.get(index);
schedule.get(index).alarm.cancel(m.intent);
schedule.remove(m);
}
#Override
public void cancelAllMessages() throws RemoteException {
for(TimedMessage m : schedule){
m.alarm.cancel(m.intent);
}
schedule.clear();
}
};
return mBinder;
The manifest:
<service
android:name="com.pearhill.messagesender.SendMessageService"
android:enabled="true"
android:exported="true"
android:process=":remote" >
<intent-filter>
<action android:name="com.pearhill.messagesender.ISendMessageService.aidl" />
</intent-filter>
</service>
Here are the links for full description:
https://developer.android.com/guide/components/bound-services.html
https://developer.android.com/guide/components/aidl.html#Calling
As a key point make sure your aidl is copied over to your application properly. Clean the project and remove aidl.
Then copy the aidl over to your application in aidl folder, but package name same of remote aidl. Rebuild. You need not to mention service in your androidManifest of calling application; it will be taken care by aidl interface that you copied over.
Make sure service is also an app and not a library (jar type). At least we couldn't make it work with service in jar file. You may choose "No activity" in the new app wizard, if you are not planning to create local activity. In that case you can create apk using "Build->Build Apk" menu in Android studio for service based app. Make sure to install the service apk before running BindService from remote app. You need not to run service as BindService will also start the service, but you need to install apk, so that it is available in system. You can use adb install path/myapkname.apk.
Action name is not must, but this is how we did bind the service, where "com.example.RemoteService.BIND" was action name for service:
String pkg = IRemoteService.class.getPackage().getName();
//get the class name from the interface package
String interfaceName = IRemoteService.class.getName();
String clsName = interfaceName.replace("IRemoteService", "RemoteService");
Intent it = new Intent("com.example.RemoteService.BIND");
it.setClassName(pkg, clsName);
boolean bRet = getApplicationContext().bindService(it, mConnection, Service.BIND_AUTO_CREATE);
Log.d("IRemote", "IRemoteService Service.BIND_AUTO_CREATE return: " + bRet);
This is what was used in eclipse and worked, but failed in Android studio because of explicit intent:
Intent it = new Intent( );
it.setClassName("com.example.aidlservice",
"com.example.aidlservice.MyService");
//optional
it.setAction("com.example.RemoteService.BIND");
//binding to remote service
boolean bRet = bindService(it, mServiceConnection, Service.BIND_AUTO_CREATE);
Log.d("IRemote", "Service.BIND_AUTO_CREATE");
Here is manifest of service app. You may want to add permissions for security reasons:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testaidl">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<service android:name=".RemoteService" android:process=":remote" android:exported="true">
<intent-filter>
<action android:name="com.example.RemoteService.BIND" />
</intent-filter>
</service>
</application>
</manifest>

Categories

Resources