For some reason, the service is not started, none of my debug logs are being called, nor are the toasts I created within the service being shown (Toasts displayed via a handler, toast code not shown, simple guide here for reference: http://www.jjoe64.com/2011/09/show-toast-notification-from-service.html)
public class MainActivity extends Activity {
Intent locationPollingIntent;
Button updateLocationButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationPollingIntent = new Intent(this, LocationService.class);
updateLocationButton = (Button) findViewById(R.id.updateLocationButton);
updateLocationButton.setText("Start");
updateLocationButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, updateLocationButton.getText().toString());
if (updateLocationButton.getText().toString() == "Start") {
MainActivity.this.startService(locationPollingIntent);
updateLocationButton.setText("Stop");
} else if (updateLocationButton.getText().toString() == "Stop") {
MainActivity.this.stopService(locationPollingIntent);
updateLocationButton.setText("Start");
}
}
});
}
}
A snippet from my Service class looks like so:
public class LocationService extends Service {
#Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "Location service started");
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "Starting Service" );
grabLocation();
return super.onStartCommand(intent, flags, startId);
}
// No implementation
#Override
public IBinder onBind(Intent arg0) {
return null;
}
Lastly, my manifest contains the following
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rperryng.intellilocation"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<service
android:name=".LocationService"
android:label="Location Service" />
<activity
android:name="com.rperryng.intellilocation.activities.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>
</manifest>
It is also worth noting that my mainActivity is in a package com.rperryng.intellilocation.activities and my service in com.rperryng.intellilocation.backgroundServices
I found the following log that is probably related to the issue:
01-27 12:53:58.804: W/ActivityManager(262): Unable to start service Intent { cmp=com.rperryng.intellilocation/.backgroundServices.LocationService }: not found
Try specifying the full package name in your manifest android:name="com.rperryng.intellilocation.backgroundServices.LocationService"
Related
Mobile - Activity
public class TestActivity extends Activity implements DataApi.DataListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
private GoogleApiClient mGoogleApiClient;
Button syncBtn;
static int click = 0;
#Override
protected void onStart()
{
super.onStart();
mGoogleApiClient.connect();
}
#Override
protected void onPause()
{
super.onPause();
mGoogleApiClient.disconnect();
}
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
//mGoogleApiClient.connect();
syncBtn = (Button) findViewById(R.id.syncBtn);
syncBtn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
if(mGoogleApiClient.isConnected())
{
PutDataMapRequest mapRequest = PutDataMapRequest.create(Constants.RUN_UPDATE_NOTIFICATION);
mapRequest.getDataMap().putDouble(Constants.NOTIFICATION_TIMESTAMP, System.currentTimeMillis());
mapRequest.getDataMap().putString(Constants.NOTIFICATION_TITLE, "This is a Title");
mapRequest.getDataMap().putString(Constants.NOTIFICATION_CONTENT, "This is a text with some, notification, see click: "+click++);
PutDataRequest request = mapRequest.asPutDataRequest();
Wearable.DataApi.putDataItem(mGoogleApiClient, request).setResultCallback(new ResultCallback<DataApi.DataItemResult>()
{
#Override
public void onResult(DataApi.DataItemResult dataItemResult)
{
if (dataItemResult.getStatus().isSuccess())
{
System.out.println(" syncing successful...."+dataItemResult.getStatus());
}
else
{
System.out.println(" syncing failed.."+dataItemResult.getStatus());
}
}
});
}
else
{
System.out.println("not connected....");
}
}
});
}
#Override
public void onConnected(Bundle bundle)
{
}
#Override
public void onConnectionSuspended(int i)
{
}
#Override
public void onDataChanged(DataEventBuffer dataEventBuffer)
{
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult)
{
}
}
Getting output from Mobile - activity -
System.outīš syncing successful....Status{statusCode=SUCCESS, resolution=null}
But it show no response on Wear - activity(Want to show something on android wear that device has synced).
Below is the code for Wear - activity
public class NotificationUpdateService extends WearableListenerService{
private int notificationId = 001;
#Override
public void onDataChanged(DataEventBuffer dataEvents)
{
super.onDataChanged(dataEvents);
System.out.println("****** ");
for(DataEvent dataEvent: dataEvents)
{
if(dataEvent.getType() == DataEvent.TYPE_CHANGED)
{
DataMap dataMap = DataMapItem.fromDataItem(dataEvent.getDataItem()).getDataMap();
String title = dataMap.getString("title");
String content = dataMap.getString("content");
System.out.println("title: "+title+" content: "+content);
sendNotification(title, content);
}
}
}
private void sendNotification(String title, String content)
{
Intent viewIntent = new Intent(this, MainActivity.class);
PendingIntent pendingViewIntent = PendingIntent.getActivity(this, 0, viewIntent, 0);
// this intent will be sent when the user swipes the notification to dismiss it
/* Intent dismissIntent = new Intent(Constants.ACTION_DISMISS);
PendingIntent pendingDeleteIntent = PendingIntent.getService(this, 0, dismissIntent, PendingIntent.FLAG_UPDATE_CURRENT);*/
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setContentText(content)
.setContentIntent(pendingViewIntent);
//.setDeleteIntent(pendingDeleteIntent)
Notification notification = builder.build();
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
notificationManagerCompat.notify(notificationId++, notification);
}}
Android manifest.xml - Wear
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.speedometer" >
<uses-feature android:name="android.hardware.type.watch" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.DeviceDefault" >
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<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=".NotificationUpdateService">
<intent-filter>
<action android:name="com.google.android.gms.wearable.BIND_LISTENER" />
</intent-filter>
</service>
</application></manifest>
Below is the Android manifest for Mobile
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.speedometer" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#mipmap/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>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name=".LocationActivity"
android:label="#string/title_activity_location" >
</activity>
<activity
android:name=".TestActivity"
android:label="#string/title_activity_test" >
</activity>
</application></manifest>
You have the line "mGoogleApiClient.connect();" commented out and therefor are not guaranteed to have usable access to the play services WearableAPI
Additionally there are two main reasons why onDataChanged won't be triggered on an android wear device 1) The DataItem you are "putting" hasn't been changed since the last time you put it into the DataLayer. Try adding a timestamp to avoid this issue for debugging purposes. 2) The package name and signature are not identical on the wear apk and mobile apk. Do all you can to ensure these are identical on both modules.
Make sure both the wearable and handheld app modules have the same package name and version number. Also check the applicationId , versionName and versionCode in build.gradle files on both the wearable and handheld app modules of your project if you are building with Gradle.
I have been trying to workout this problem for the past couple of hours looking at various code samples here but still haven't found a solution.
I want a service to start when app starts and when the phone is reboot the service should start again automatically without starting the app. (Neither the toast or log appears when i restart my phone). Please see my code below:
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, MyService.class);
startService(intent);
}
MyService.java
public class MyService extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startid){
Toast.makeText(this, "Service Running ...", Toast.LENGTH_LONG).show();
Log.v("Service","Service Running ...");
return super.onStartCommand(intent, flags, startid);
}
}
Autostart.java
public class Autostart extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent(context, MyService.class);
context.startService(myIntent);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xxxxx.receiverservice" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<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>
<receiver
android:name=".Autostart"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".MyService" />
</application>
</manifest>
Seems like this is a bug on Xiaomi. I haven't encountered this problem with any other device
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...
Probably an easy question for you guys, but its my first attempt at creating a service that should run in the background when my app closes. My problem is: The service doesnt start when I click on my "start service" button. I don't see any of the toast, and nothing in the logcat (no errors either). Thanks in advance!
Service class
public class MyService extends Service {
private static final String TAG = "MyService";
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
}
#Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
#Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
}
}
My main activity
public void startServiceClick(View v){
Log.d("foo", "onClick: starting srvice");
startService(new Intent(this, MyService.class));
}
public void stopServiceClick(View v){
Log.d("foo", "onClick: stopping srvice");
stopService(new Intent(this, MyService.class));
}
Manifest xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="se.johanberntsson.main"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<application
android:icon="#drawable/maps1"
android:label="#string/app_name" >
<uses-library android:name="com.google.android.maps" />
<activity
android:name=".LongitudeActivity"
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=".DebugActivity" />
<activity android:name=".GoogleMapsActivity" />
<service android:enabled="true" android:name=".MyService" />
</application>
</manifest>
this is suggestion as all else looking fine
should call super in each function like super.onCreate();
as may be possible service is already started and try to stop and start again and check it toast appears ....
try
<service android:enabled="true" android:name="se.johanberntsson.servies.MyService" />
You may have already figured this out. In your code you override several native functions so you could add a toast and a catlog entry. Don't forget to add super.{whatever function you are calling}. What that does is it tells the android device to do whatever the function would have done had the function not been overrided, then do the extra stuff. In your case, if you call your oncreate function, it then the computer will make the toast and add a catlog entry, but it will not create the service.
#Override //forget about doing whatever onCreate usually does.
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();//toast
Log.d(TAG, "onCreate");//Add message to cat- log
//note: Service not created because function was overrided
}
however, if you add super.onCreate, the phone will create the service, then it will do what you tell it to do.
#Override //forget about doing whatever onCreate usually does.
public void onCreate() {
super.onCreate(); // do whatever onCreate usually does. Then do whatever is after me.
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();//toast
Log.d(TAG, "onCreate");//Add message to cat- log
}
I hope this helps.
I added your service into my code and it is working fine for me. please see below code and match with yours.
Activity >>
public class TestActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button bt = (Button)findViewById(R.id.button11);
bt.setOnClickListener(this);
}
public void startServiceClick(View v){
Log.d("foo", "onClick: starting srvice");
startService(new Intent(this, MyService.class));
}
public void stopServiceClick(View v){
Log.d("foo", "onClick: stopping srvice");
stopService(new Intent(this, MyService.class));
}
#Override
public void onClick(View v) {
startServiceClick(v);
}
Manifest File >>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TestActivity"
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:enabled="true" android:name=".MyService" />
</application>
I have a problem that I made a service which executes a task in background after every 5 seconds, for this I made a receiver in manifest and autostarter class but it is not excuted in my app. Means the service is not running in the app. I don't know why? Please suggest me for the right answer.
Android Manifest:
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name="com.halosys.TvAnyTime.ServiceAutoStarter">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<activity android:name=".MainScreen"
android:label="#string/app_name" android:screenOrientation="portrait" android:theme="#android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".IntroVideo" android:label="#string/app_name" android:theme="#style/CustomTheme" android:screenOrientation="landscape"/>
<activity android:name=".WelcomeScreen1" android:theme="#android:style/Theme.NoTitleBar" android:screenOrientation="portrait"/>
<activity android:name=".IntroFaceBookScreen" android:theme="#android:style/Theme.NoTitleBar" android:screenOrientation="portrait"/>
</application>
<uses-permission xmlns:android="http://schemas.android.com/apk/res/android"
android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE"/>
<uses-permission android:name="android.permission.READ_INPUT_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-sdk android:minSdkVersion="3" />
</manifest>
ServiceAutoStarter:
public class ServiceAutoStarter extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent(context, ServiceTemplate.class);
myIntent.putExtra("extraData", "somedata");
context.startService(myIntent);
}
}
ServiceTemplate.java:
public class ServiceTemplate extends Service {
Runnable refresher;
public static int HashesInPattern=32; // 32 64byte hashes in the pattern
public static int BytesInHash =64;
static byte[] hashPattern;
ArrayList<byte[]> finalHashafterXor = new ArrayList<byte[]>();
byte[] finaldatafile = new byte[2097152];
byte[] finalhash;
InputStream is;
byte[] bytesafterXor,bytes;
String strLine;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
//code to execute when the service is first created
}
#Override
public void onDestroy() {
//code to execute when the service is shutting down
}
#Override
public void onStart(Intent intent, int startid) {
//code to execute when the service is starting up
final Handler handler = new Handler();
refresher = new Runnable() {
public void run() {
// some action
try{
Encrypt();
Toast.makeText(getApplicationContext(), "Hello World", Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
e.printStackTrace();
}
handler.postDelayed(refresher, 2000);
}
};
}
Thanks in advance.
I haven't gone through whole of your code but i saw manifest file
Like activities (and other components), you must declare all services in your application's manifest file.
Please do change and try.