Application won't bind to service - android

I have an application A that has to bind to a service that is in another package. I have put a custom intent filter in order to make it work. Sadly the application wont bind. The log says that it can't find the service.
Application A is in the package "com.example.app_a"
The service is in another package "com.example.app_talker_service"
So I just can't refer to the service with the xxx.class solution, so my guess was to use an intent filter in the manifest file of the service's package.
Application A, on the other hand, will need to do a bind to the service to make it start (if it hasn't already started) and that later it will communicate with it though the use of broadcast receivers. I did some experimentation and I noticed that the broadcasts work fine, but what is wrond is that for some reason, the application A can't seem to find my service during the binding....
Here is the code for application A which binds in onStart():
#Override
protected void onStart()
{
// TODO Auto-generated method stub
super.onStart();
//Bind to service
getApplicationContext().bindService(new Intent("com.example.talker_service.SERVICE"), mConnection,Context.BIND_AUTO_CREATE);
}
private boolean mIsBound = false;
private ServiceConnection mConnection = new ServiceConnection()
{
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
mIsBound = true;
Toast.makeText(getApplicationContext(), "CONNECTED TO SERVICE!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.setAction("com.example.talker_service.SERVICE");
intent.putExtra("REQUEST", "REGISTER APP");
intent.putExtra("FILTER", "com.example.app_a");
intent.putExtra("NAME", "Applicazione A");
String[] components = {"NUMBER_SENT","CHANGE_TEXT_COLOR","CHANGE_TEXTVIEW_SIZE"};
intent.putExtra("COMPONENTS", components);
MainActivityA.this.sendBroadcast(intent);
}
#Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
mIsBound = false;
}
};
Here instead is the cmanifest for the service which I called Talker_service:
en<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app_talker_service"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<service
android:name=".Talker_Service"
android:enabled="true"
android:exported="true"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE" >
<intent-filter >
<action android:name="com.example.talker_service.SERVICE"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain" />
</intent-filter>
</service>
<activity
android:name=".ConnectionManagerActivity"
android:label="#string/title_activity_connection_manager" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I don't undertsand why it doesn't bind.. I put the intent filter, am I missing something? Ah, and the logs says this:
12-03 22:45:13.786: W/ContextImpl(26076): Implicit intents with startService are
not safe: Intent {
act=com.example.talker_service.SERVICE }
android.content.ContextWrapper.bindService:517
com.example.app_a.MainActivityA.onStart:81
android.app.Instrumentation.callActivityOnStart:1171
12-03 22:45:13.786: W/ActivityManager(764): Unable to start service Intent { >act=com.example.talker_service.SERVICE } U=0: not
found
This instead is the manifest file for application A
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app_a"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.BIND_ACCESSIBILITY_SERVICE" ></uses-permission>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivityA"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

I don't know why using intent filters is not working (it says that it can't find it) but by using the following code I was able to make my application connect to the remote service:
Intent i = new Intent();
i.setClassName("com.example.app_talker_service", "com.example.app_talker_service.Talker_Service");
bindService(i, conn, Context.BIND_AUTO_CREATE);
so using the seClassName method and providing the name of the pacake and that full path of the service, I was able to make it work. I am not sure if this falls in the realm of "best practice" for this type of problem, but for my it worked. Any solution is better than no solution. I found this solution thants to this link:
Remote Service Tutorial

Related

Android: onServiceConnected not called after bindService

None of the many similar questions I have found have helped to solve my issue.
Here are some questions I've looked at:
ServiceConnection.onServiceConnected() never called after binding to started service
onServiceConnected never called after bindService method
ServiceConnection::onServiceConnected not called even though Context::bindService returns true?
OnServiceConnected not getting called
Here is part of my android app code:
//Local service interface
private INetworkQueryService myNetworkQueryService = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
Intent intent = new Intent(this, NetworkQueryService.class);
startService (intent);
bindService (new Intent(this, NetworkQueryService.class), myNetworkQueryServiceConnection,
Context.BIND_AUTO_CREATE);
}
//Service connection
private final ServiceConnection myNetworkQueryServiceConnection = new ServiceConnection() {
/** Handle the task of binding the local object to the service */
public void onServiceConnected(ComponentName className, IBinder service) {
myNetworkQueryService = ((NetworkQueryService.LocalBinder) service).getService();
// as soon as it is bound, run a query.
loadNetworksList();
}
/** Handle the task of cleaning up the local binding */
public void onServiceDisconnected(ComponentName className) {
myNetworkQueryService = null;
}
public void loadNetworksList() {
// delegate query request to the service.
try {
myNetworkQueryService.startNetworkQuery(myCallback);
} catch (RemoteException e) {
}
}
};
/**
* This implementation of INetworkQueryServiceCallback is used to receive
* callback notifications from the network query service.
*/
private final INetworkQueryServiceCallback myCallback = new INetworkQueryServiceCallback.Stub() {
#Override
public void onQueryComplete(List<NetworkInfo> networkInfoArray,
int status) throws RemoteException {
displayNetworks(networkInfoArray, status);
}
/** place the message on the looper queue upon query completion. */
};
As you can sy, myNetworkQueryServiceConnection should be called from within the bindService method (located in onCreate()). This (in theory) should also run onServiceConnected (I think), but I put a breakpoint in my code and it never stops inside that method. I did some debugging and bindService returns false, so for some reason it's not successfully binding it.
This is my first time ever having to bind services in Android, so I'm sure I've missed something. I'm hoping someone could point out a mistake.
Let me know if you're missing any information--I'm not sure what all you might need, and I really can't post my entire app code here.
EDIT: I keep reading things about registering the service in the manifest but cannot seem to figure out where or how that would be done? Here's the manifest for this project:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tek15.cellemrmonitor"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I certainly don't see anything about the service, but I guess I don't know how to add it.
You need to add a <service> element, as a peer of your <activity> element, as a child of <application>:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tek15.cellemrmonitor"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="NetworkQueryService" />
</application>
My example element assumes that NetworkQueryService is really com.tek15.cellemrmonitor.NetworkQueryService — otherwise, substitute in the proper fully-qualified class name.

Autostart app immediately after the boot

I want to start my application when phone startup
I just follow tutorial from here but it doesn't work in my device. Please see my method:
package net.londatiga.android;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, ExampleActivity.class);
context.startService(startServiceIntent);
}
}
And this is my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.londatiga.android"
android:versionCode="2" android:versionName="1.01">
<uses-sdk android:minSdkVersion="7"
android:targetSdkVersion="15"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name="net.londatiga.android.MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="1000">
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity android:name=".ExampleActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Where is my mistake please?
Instead of:
context.startService(startServiceIntent);
Use:
context.startActivity(startServiceIntent);
You don't have any Service, You need to open activity.
Intent startServiceIntent = new Intent(context, ExampleActivity.class);
context.startActivity(startServiceIntent);
Create a class name it as AfterBootActivity:
public class AfterBootActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}}
Now, Create a new class named as Autostart.java which extends a BroadcastReceiver:
public class Autostart extends BroadcastReceiver {
public void onReceive(Context context, Intent arg1) {
Intent intent = new Intent(context, StarterService.class);
context.startService(intent);
}}
In the Manifest file add this class as a receiver. This class will listen to the Broadcast call the Android OS sends after the boot sequence has finished i.e. after the phone started up.
Now Create a class named as StarterService.java which will extend Service:
public class StarterService extends Service {
private static final String TAG = "MyService";
public IBinder onBind(Intent intent) {
return null;
}
public void onDestroy() {
Toast.makeText(this, "Service stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
/**
* The below started service opens the Activity.
*/
public void onStart(Intent intent, int startid) {
Intent intents = new Intent(getBaseContext(), AfterBootActivity.class);
intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intents);
Toast.makeText(this, "Service started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
}}
When the class Autostart receives the BOOT_COMPLETED Broadcast from Android OS it will start the StarterService which then starts the Android Activity “AfterBootActivity” i.e our main class. We can play any audio/video or anything in it.
Change your Manifest.xml as below:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="on.boot.completed"
android:installLocation="internalOnly"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="on.boot.completed.AfterBootActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="on.boot.completed.Autostart" >
<intent-filter>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name="on.boot.completed.StarterService"
android:enabled="true"
android:exported="true" />
</application>
</manifest>
Also Remember to install it in internal memory because if the app installed on the SD Card then autostart will not work! That’s why it’s important that we add in manifest.
android:installLocation="internalOnly"
That’s all run your app.
After it has been started turn off your phone and turn it back on and the app would start automatically after the device has booted up.

Activity won't start a service

I m trying to start an IntentService from the main activity of y application and it won't start. I have the service in the manifest file. Here's the code:
MainActivity
public class Home extends Activity {
private LinearLayout kontejner;
IntentFilter intentFilter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
kontejner = (LinearLayout) findViewById(R.id.kontejner);
intentFilter = new IntentFilter();
startService(new Intent(getBaseContext(), HomeService.class));
}
}
Service:
public class HomeService extends IntentService {
public HomeService() {
super("HomeService");
// TODO Auto-generated constructor stub
}
#Override
protected void onHandleIntent(Intent intent) {
Toast.makeText(getBaseContext(), "TEST", Toast.LENGTH_LONG).show();
}
}
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.salefinder"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Home"
android:label="#string/title_activity_home" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".HomeService" />
</application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
How can I make it work?
onHandleIntent gets called from a background thread. You can't modify the UI, or in this case, make Toast from outside the UI thread. So, I wouldn't expect anything to happen with your service.
Just try writing something out with Log.d() to see if your service is getting called.
It seams that android cached a bad version of the app - I forced closed it and started it again, and it worked...

IntentService is not being called

I am trying to send an Intent from the onCreate in an Activity to start an IntentService. However the IntentService's onHandleIntent is never being received. I have tried changing around the Manifest with Intent-filters but nothing seems to be working. No exceptions are being thrown, but the IntentService is simply not called.
Here is the onCreate
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
db = new TwitterDB(this);
String[] columns = new String[1];
columns[0] = TwitterDB.TEXT;
tAdapter = new SimpleCursorAdapter(this, 0, null, columns, null, 0);
tweets = (ListView)findViewById(R.id.listtwitter);
tweets.setAdapter(tAdapter);
Intent intent = new Intent(this, SyncService.class);
intent.putExtra("action", SyncService.TWITTER_SYNC);
this.startService(intent);
}
Here is the creator and onHandleIntent of the IntentService class, I know it is not being called because logcat never shows "Retrieved intent". The constructor is not called either (There was a log call in there, but I removed it.
public SyncService(){
super("SyncService");
twitterURI = Uri.parse(TWITTER_URI);
Log.i("SyncService", "Constructed");
}
#Override
public void onHandleIntent(Intent i){
int action = i.getIntExtra("action", -1);
Log.i("SyncService", "Retrieved intent");
switch (action){
case TWITTER_SYNC:
try{
url = new URL(twitterString);
conn = (HttpURLConnection)url.openConnection();
syncTwitterDB();
conn.disconnect();
}catch(MalformedURLException e){
Log.w("SyncService", "Twitter URL is no longer valid.");
e.printStackTrace();
}catch(IOException e){
Log.w("SyncService", "Twitter connection could not be established.");
e.printStackTrace();
}
break;
default:;
// invalid request
}
}
And here is the Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.twitter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<application android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TwitterTwoActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<service android:name="SyncService"/>
</activity>
</application>
</manifest>
Move your service declaration outside of the scope of the TwitterTwoActivity in the XML file, as I have done here:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.twitter"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<application android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TwitterTwoActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="SyncService"/>
</application>
</manifest>
You need to define the path to the service in your manifest, simply putting the name is not sufficient.
Change
<service android:name="SyncService"/>
to
<service android:name=".SyncService"/>
if SyncService is in the root of your package, add respective folder before .SyncService if needed.

broadcast receiver error custom intent

I'm doing something I thought would be simple as have done a broadcast receiver in the past on 2.3.4 droid. I'm using 4.x now in this little program.
Here is the main architecture/flow. I want/have a receiver to be listening for a custom broadcast/intent. I created this as a stand along project/apk
public class FileIntegrityMonitor extends BroadcastReceiver
{
public String CLASS_NAME = "FileIntegrityMonitor";
#Override
public void onReceive(Context context, Intent intent)
{
Log.d(CLASS_NAME, "Entered onReceive: got broadcast from startFIM");
String actionReceived = intent.getAction();
Log.d(CLASS_NAME, "Action received=" + actionReceived);
if (actionReceived.equals(StartFIMActivity.CUSTOM_INTENT))
{
//start service to perform the file integrity check
Log.d(CLASS_NAME, "start service to perform file integrity check");
}
}
Then I created a activity as a separate project/app as a driver for now to kick the broadcast receiver off. Will probably replace with something else later but wanted to get the mechanics/comms down now.
public class StartFIMActivity extends Activity
{
/** Called when the activity is first created. */
public static final String CUSTOM_INTENT = "com.kdms2.StartFIM.intent.action.start";
public String CLASS_NAME = "StartFIMActivity";
#Override
public void onCreate(Bundle savedInstanceState)
{
Log.d(CLASS_NAME, "Entered onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i = new Intent();
i.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); //api 3.1+
i.setAction(CUSTOM_INTENT);
Log.d(CLASS_NAME, "send brodcast to monitor");
this.sendBroadcast(i);
}
}
Now in the trace I do get the intent in FileIntegrityMonitor but a strange message that it's trying to run some method and I don't know why. Error is:
E/AndroidRuntime(979): java.lang.RuntimeException: Unable to instantiate receiver com.kdms2.FileIntegrityMonitor.StartFIMActivity: java.lang.ClassNotFoundException: com.kdms2.FileIntegrityMonitor.StartFIMActivity
why did it add the class name of the activity that broadcast the action to the broadcast receiver?
Is it something in the manifest (receiver)?
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<receiver android:name=".StartFIMActivity" android:enabled="true">
<intent-filter android:priority="99999999999">
<action android:name="com.kdms2.StartFIM.intent.action.start"/>
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
</manifest>
activity manifest
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".StartFIMActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
</application>
</manifest>
v/r,
Frank
Your receiver's name is FileIntegrityMonitor, but in the manifest you try to register StartFIMActivity as both the <activity> and <receiver> elements. Probably oughta be:
<receiver android:name=".FileIntegrityMonitor" android:enabled="true">
<intent-filter android:priority="99999999999">
<action android:name="com.kdms2.StartFIM.intent.action.start"/>
</intent-filter>
</receiver>
Or something of the like.
HTH

Categories

Resources