Unable to start Service Intent - android

I have a service class. I have exported this class to jar and I have embed the jar in my client app.
When needed, I call the service class. When I try to do this, I get the following error:
Unable to start service Intent {comp={com.sample.service/com.sample.service.serviceClass}} : not found
I have other class apart from the service class, which I am able to access (create object of that class) which are inside the same jar.
I feel I have missed out some thing in my configuration or manifest or so.
Please help me identifying the same. My code is below:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent () ;
intent.setClassName("com.sample.service" ,"com.sample.service.serviceClass") ;
this.startService(intent) ; // when I call this line I get the message...
// binding other process continue here
}
Client manifest.xml
<service android:name="com.sample.service.serviceClass"
android:exported="true" android:label="#string/app_name"
android:process=":remote">
<intent-filter><action android:name="com.sample.service.serviceClass"></action>
</intent-filter>
</service>

For anyone else coming across this thread I had this issue and was pulling my hair out.
I had the service declaration OUTSIDE of the '< application>' end tag DUH!
RIGHT:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...>
...
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity ...>
...
</activity>
<service android:name=".Service"/>
<receiver android:name=".Receiver">
<intent-filter>
...
</intent-filter>
</receiver>
</application>
<uses-permission android:name="..." />
WRONG but still compiles without errors:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...>
...
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity ...>
...
</activity>
</application>
<service android:name=".Service"/>
<receiver android:name=".Receiver">
<intent-filter>
...
</intent-filter>
</receiver>
<uses-permission android:name="..." />

First, you do not need android:process=":remote", so please remove it, since all it will do is take up extra RAM for no benefit.
Second, since the <service> element contains an action string, use it:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent=new Intent("com.sample.service.serviceClass");
this.startService(intent);
}

1) check if service declaration in manifest is nested in application tag
<application>
<service android:name="" />
</application>
2) check if your service.java is in the same package or diff package as the activity
<application>
<!-- service.java exists in diff package -->
<service android:name="com.package.helper.service" />
</application>
<application>
<!-- service.java exists in same package -->
<service android:name=".service" />
</application>

I hope I can help someone with this info as well:
I moved my service class into another package and I fixed the references. The project was perfectly fine, BUT the service class could not be found by the activity.
By watching the log in logcat I noticed the warning about the issue: the activity could not find the service class, but the funny thing was that the package was incorrect, it contained a "/" char. The compiler was looking for
com.something./service.MyService
instead of
com.something.service.MyService
I moved the service class out from the package and back in and everything worked just fine.

In my case the 1 MB maximum cap for data transport by Intent.
I'll just use Cache or Storage.

I've found the same problem. I lost almost a day trying to start a service from OnClickListener method - outside the onCreate and after 1 day, I still failed!!!! Very frustrating!
I was looking at the sample example RemoteServiceController. Theirs works, but my implementation does not work!
The only way that was working for me, was from inside onCreate method. None of the other variants worked and believe me I've tried them all.
Conclusion:
If you put your service class in different package than the mainActivity, I'll get all kind of errors
Also the one "/" couldn't find path to the service, tried starting with Intent(package,className) and nothing , also other type of Intent starting
I moved the service class in the same package of the activity
Final form that works
Hopefully this helps someone by defining the listerners onClick inside the onCreate method like this:
public void onCreate() {
//some code......
Button btnStartSrv = (Button)findViewById(R.id.btnStartService);
Button btnStopSrv = (Button)findViewById(R.id.btnStopService);
btnStartSrv.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startService(new Intent("RM_SRV_AIDL"));
}
});
btnStopSrv.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
stopService(new Intent("RM_SRV_AIDL"));
}
});
} // end onCreate
Also very important for the Manifest file, be sure that service is child of application:
<application ... >
<activity ... >
...
</activity>
<service
android:name="com.mainActivity.MyRemoteGPSService"
android:label="GPSService"
android:process=":remote">
<intent-filter>
<action android:name="RM_SRV_AIDL" />
</intent-filter>
</service>
</application>

Related

Can't resolve symbol 'SecondActivity' Validates resource references inside Android XML files

I'm new to Android Application development. I just added another activity tag in my AndroidManifest.xml file but its gives me the following message : Can't resolve symbol 'SecondActivity' Validates resource references inside Android XML files.
I want to know why is this message getting displayed?
Here is my AndroidManifest.xml file :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.aupadhyay.myfirstapp">
<application
android:allowBackup="true"
android:icon="#mipmap/amiticon"
android:label="#string/app_name"
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>
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="anything" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
And this is my SecondActivity.java file :
import android.support.v7.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity {
}
Picture :
Consider the following:
Remove the intent-filter inside your second activity unless you really need that.
In your second activity, override onCreate method
#override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity_layout);
}
Did you create a layout for your second activity? It seems your activity is empty - although this might not be the cause of your problem. Also what is the location of your SecondActivity, a different package or in the same place as the first activity?
As shown in your code, your second activity doesn't seem to have any of that layout stuff!
I hope this helps you!
It seems your SecondActivity doesn't belongs to the package "com.aupadhyay.myfirstapp", So move it to src of the "com.aupadhyay.myfirstapp" package where ever it currently belongs to.

Android: Boot Complete only works if I launch App Immediately

I'm trying to reset alarms in my app and using a receiver to get onBootCompleted. To see if the intent was received, I'm using a toast. The toast only appears if I immediately open the app. Otherwise, the toast does not appear. I looked at previous questions but almost all of them involve services, which I am not using. I am not sure if that is a part of the problem.
Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:installLocation="internalOnly"
package="package.name" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:configChanges="orientation|screenSize|keyboardHidden"
android:name=".MainActivity"
android:label="#string/app_name"
android:launchMode="singleTop" >
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
<receiver android:name=".AlarmReceiver" >
<intent-filter>
<action android:name="android.intent.action.ALARM_SERVICE" />
</intent-filter>
</receiver>
<receiver android:name=".AlarmReset"android:enabled="true" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
Receiver Class
public class AlarmReset extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Hello! Got message",
Toast.LENGTH_LONG).show();
//reset alarms etc. No service set.
}
I also tried writing the manifest receiver as
<receiver android:name=".AlarmReset" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Key points I found online were to include permissions (which I did) and to watch out for logging.
What I do not understand is why it works if I immediately (within a few seconds, otherwise the toast does not appear) start my activity but is unsuccessful otherwise. I am considering testing a few possibilities like launching the activity itself through code or using a service like most others have. I am currently testing on Android 4.4 on an actual phone.
When an app is installed, it is in a stopped state. None of its components will be activated (such as your BOOT_COMPLETED receiver) until the app is moved out of this state by being launched by the user. This is why your app doesn't work unless you launch it once.
Note that force stopping the app from Settings also moves it into this stopped state.
See this page for more details (search the page for "launch controls").
Starting with Android 3.1 all applications, upon installation, are placed in a "stopped" state.(This is the same state that the application ends up in after the user force-stops the app from the Settings application.)
While in "stopped" state, the application will not run for any reason, except by a manual launch of an activity. (Meaning no BroadcastReceviers(ACTION_PACKAGE_INSTALLED, BOOT_COMPLETED etc.) will be invoked, regardless of the event for which they have registered, until the user runs the app manually.)
But you can start a serivice for ex-
1) In your element:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
2) In your element (be sure to use a fully-qualified [or relative] class name for your BroadcastReceiver):
<receiver android:name="com.example.MyBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
In MyBroadcastReceiver.java:
package com.example;
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent startServiceIntent = new Intent(context, MyService.class);
context.startService(startServiceIntent);
}
}
I feel the need to clarify the issue and solution, since the question was not clear (due to confidentiality issues).
The Basic Solution to my problem was just starting a service.
The Problem: I was trying to make the alarm in my class, AlarmReset, through AlarmManager. That in itself may have been an issue, but in addition, I tried to access objects that were instantiated in the MainActivity, furthering my dilemma. The reason why it worked when I opened MainActivity quickly enough, I suspect, is because I was able to instantiate the objects and set up the prerequisites for the class to directly access. I think the toast not appearing is similar to the issue.
The Solution: I set up a service class which I directed AlarmReset to. This is what I changed the AlarmReset class to:
public class AlarmReset extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Hello! Got message",
Toast.LENGTH_LONG).show();
//Pretty sure the Toast doesn't appear still.
Intent service = new Intent(context, Service.class);
context.startService(service);
}
Then my service class
public class Service extends IntentService {
private DataBaseManager database;
public Service()
{
super("Service");
}
#Override
protected void onHandleIntent(Intent intent)
{
database = new DataBaseManager(this);
Toast.makeText(this, "Hi",
Toast.LENGTH_LONG).show();
Toast.makeText(this, "Hello! Got message",
Toast.LENGTH_LONG).show();
//rest of code
}
Similarly, the text does not appear except if I am on the app immediately (I suspect it has to do with the threads).
Here, I made sure to instantiate my objects before using them (or did so after crashing).
A few possible problems that others may encounter (as I have read) are the following:
Installing in internal storage
android:installLocation="internalOnly"
and receiving permission for booting
<uses-permission
android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Some problems that people had suggested and were not very important were:
Using android:enabled and android:exported. Default values are fine
Writing the full receiver name. For me it wasn't necessary
<action android:name="android.intent.action.QUICKBOOT_POWERON" /> did not seem to do much.

App refuses to connect to Parse backend

I am working on an app called TobaccoRoad that uses a library project called BestApproach. It uses a Parse backend to display custom generated content and handle push notifications. Everything was working pretty alright until a few days ago, when I must have messed up some settings somewhere and it no longer seems to be making the connection to the parse systems. I'm quite sure it's a local issue, because my second tester phone, which has not had updated code pushed to it in a few days, is still receiving notifications and can view that custom content.
The weird thing is, even after clearing my workspace and starting fresh from the (definitely good) code my employer gave me, and following all the tutorials and troubleshooting guides on Parse.com (see https://parse.com/docs/push_guide#installations/Android; https://parse.com/tutorials/android-push-notifications) I'm still not connecting to Parse. I haven't made any significant changes that I can recall, so I'm at a loss as to what might be causing this.
I know it's not an issue of a bad applicationID or clientKey, because even substituting random strings into the Parse.initialize call gave the same results, and a logcat error about not being able to authenticate.
Here are the relevant bits from my manifest files, first for the library project...
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bestapproach.lib"
android:versionCode="8"
android:versionName="1.6.1">
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:icon="#drawable/app_icon" android:label="#string/app_name"
android:theme="#style/Theme.BA" >
<activity android:name="com.bestapproach.lib.SplashActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="orientation"
android:theme="#style/Theme.BA.Splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--Declarations for all of my Activities...-->
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
</application>
</manifest>
And the manifest is exactly the same for my dependent project, with the exception of where I define a custom receiver at the end:
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver android:name="com.bestapproach.lib.MyCustomReceiver">
<intent-filter>
<action android:name="com.bestapproach.lib.UPDATE_STATUS" />
</intent-filter>
</receiver>
And here's the code for the onCreate() method in my main activity (SplashActivity) where the Parse service is initialized:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
String parseClientId = getString(R.string.parse_client_id);
String parseAppId = getString(R.string.parse_app_id);
//debug output
Log.v("parse should be initializing...", parseAppId+" "+parseClientId);
if (!("".equals(parseClientId) || "".equals(parseAppId))) {
Parse.initialize(this, parseAppId, parseClientId);
PushService.subscribe(this, "", MenuActivity.class);
PushService.setDefaultPushCallback(this, SplashActivity.class);
ParseInstallation.getCurrentInstallation().saveInBackground();
ParseAnalytics.trackAppOpened(getIntent());
final Activity a = this;
// Fetches content if it doesn't exist.
StoreManager sm = StoreManager.getInstance(a);
ParseStoreManager psm = ParseStoreManager.getInstance(a);
return;
}
}
Suggestions I've found that seem like they may be on track with what I need include running Parse.initialize() in the onCreate() of every activity, which I don't really want to do as there are a lot of them and that would be a lot of duplicated code, or generating an Application object and running it from there. Everything I've tried in relation to that has ended up breaking once I add it to my manifest file, due to TobaccoRoad's dependencies on the library project.
I know, it's a lot to dig through, but any suggestions would be appreciated. Thanks everybody.
Possible fix:
Change this line
if (!("".equals(parseClientId) || "".equals(parseAppId))) {
Parse.initialize(this, parseAppId, parseClientId);
to this:
if (!("".equals(parseClientId) || "".equals(parseAppId))) {
Parse.initialize(SplashActivity.this, parseAppId, parseClientId);
the issue is that
ParseAnalytics.trackAppOpened(getIntent());
accepts the intent from that activity from your SplashActivity and from the application scope
Also, you initialize parse from the activity which we generally don't do.
We try initialize parse from the Application class so it has the context of the Application scope and not of the Activity Scope.
I recommend you to create an Application class and include the parse code in the onCreate of the Application...which you would need to do only once.
Or, you can create some BaseActivities and make all your activities in the application extend to that. This will save you from writing duplicate code...this is just in case you are bound not to create an Application class.
Pardon me for anything wrong...I am new in answering.

ServiceTestCase is not launching my service

I am attempting to write some JUnit tests for my android application. The application is a service.
I have tried several things to get the ServiceTestCase to launch the service but it fails to.
HOWEVER, when i debug the ServiceTestCase it WILL launch the service. I belive this is because the ServiceTestCase is calling setup and not giving enough time for the service to launch before it kills it...
I am not completely sure, this is the first time i have ever used junit testing for android. VERY new to this. Any suggestions on what i can do to fix this problem?
I was thinking of creating some timer loop or something, but that seems REALLY dirty. Would like a much cleaner aproach.
Android Manifest for the service.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dataservice.server"
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<service android:name="dataservice.server.DataServer" android:process=":remote" android:exported="true" android:enabled="true">
<intent-filter>
<action android:name="dataservice.DataService.BIND.1" />
</intent-filter>
<intent-filter>
<action android:name= ".DataServer" />
</intent-filter>
</service>
<receiver android:name="dataservice.server.Receiver">
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT"></action>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
</application>
</manifest>
The ServiceTestCase i am attempting.
public class Publish extends ServiceTestCase<DataServer> {
private final static String TAG = "Publish Unit Test";
private Context mSystemContext;
public Publish() {
super(DataServer.class);
}
public Publish(Class<DataServer> serviceClass) {
super(serviceClass);
}
#Override
protected void setUp() throws Exception {
// TODO Auto-generated method stub
super.setUp();
// this is where i am attempting to start the service.
// i have attempted other methods, but none of those worked either.
startService(new Intent(this.getContext(), DataServer.class));
}
public void testPublish() {
}
}
I was referencing one of the dependant projects incorrectly. Once this was fixed, it resolved the issue. So, check and make sure you are referencing your projects correctly if you are having the same issue.

Android: Can't find activity in the Manifest, but it IS there?

And i can't figure out why! I checked logcat and i saw that the reason it crashed was because it isn't finding the Activity in the manifest file for some unknown reason.
I've looked at similar threads but none of the answers seem to work for me.
Here's the code where i start the activity:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
Intent ntnt = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(ntnt);
}
}
And the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="se.jbhalmstad.ndroid"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<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>
<activity android:name=".SettingsActivity"></activity>
</application>
</manifest>
As you can see i declared the SettingsActivity that i'm trying to start, at the bottom of the Manifest, inside of the tag.
I might be blind, but i can't find anything wrong. Can you?
Let me know if you need more source, but the SettingsActivity should be irrelevant because it doesn't get that var when i run the program.
Source code
If you want to take a closer look, here's the entire source code.
http://www.speedyshare.com/files/30343046/project.zip
The problem is in your SettingsActivity
when you are using PreferenceActivity you use
addPreferencesFromResource(R.layout.settings);
to set contents.
but you are using
setContentView(R.layout.settings);
Could be package name mismatch. What Java package is the activity class in? Must match the one in the manifest, i. e. se.jbhalmstad.ndroid. Or provide full class name in the android:name attribute.
EDIT: I have a working example, and the <activity> element for my Preferences activity looks slightly different:
<activity android:name="Prefs"/>
No dot before Prefs.
Also, check if the SettingsActivity class is public. You never showed it to us.
i think you dont need to add the name of the package to your acitivity name , you can just add your activity SettingActivity like this :
<activity android:name="SettingsActivity" />
because your activity is in the same package that is declared on the manifest :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="se.jbhalmstad.ndroid"...
let me know if it helps
Be sure that Manifest intent on the right activity and not just the basic Java / Kotlin class file
New -> Activity -> Empty Activity instead of New -> Class/file
or
Use setContentView(R.layout.yours_activity_class_here); OnCreate

Categories

Resources