I have a MainActivity which do nothing and one class called BootCompletedIntentReceiver which extends BroadcastReciever and one more class with name MyServices which extends Service. All I want to do is autorun my app after device turns on which is handled by BootCompletedIntentReceiver and after that it starts a Service which shows a toast 8 times. But when i reboot my device nothing happens. I have tried my best to solve this. Can any one help me to find where i am getting stuck
Here is my BootCompletedIntentReceiver code:
package com.anshuman.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootCompletedIntentReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent pushIntent = new Intent(context, MyService.class);
context.startService(pushIntent);
}
}
}
And here is MyService class:
package com.anshuman.myapplication;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;
public class MyService extends Service {
#Override
public void onCreate() {
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId){
int a=2;
while (a<10) {
Toast.makeText(this, "onStartCommand", Toast.LENGTH_LONG).show();
a++;
}
return Service.START_STICKY;
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
And my androidManifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.anshuman.myapplication">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<receiver android:name=".BootCompletedIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<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=".MyService"/>
</application>
Code is working fine problem is that only my xiaomi device is not working with this code.
Related
I'm building a remote service and a client application targetted at API 24 executing on a Nexus 6P device. I have a remote service that automatically starts at boot. Here are the code fragments:
Remote Service Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="a.b.c">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".MyService" >
<intent-filter>
<action android:name="a.b.c.MY_INTENT" />
</intent-filter>
</service>
<activity android:name=".MyActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
Remote Service
package a.b.c;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyService extends Service
{
#Override
public IBinder onBind(Intent intent) {
return null;
// EDIT: see StackOverflow answers/comments below:
// Returning an IBinder here solves the problem.
// e.g. "return myMessenger.getBinder()" where myMessenger
// is an instance of Android's Messenger class.
}
#Override
public void onCreate() {
super.onCreate();
}
}
Remote Broadcast Receiver
package a.b.c;
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) {
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent serviceIntent = new Intent(context, MyService.class);
context.startService(serviceIntent);
}
}
}
Remote Activity
(Android Studio insists on there being an Activity)
package a.b.c;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
public class MyActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_activity);
}
}
I then have a separate project to implement a client activity in a different package that attempts to bind to the remote service. Here are the code fragments:
Client Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="x.y.z">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".ClientActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
Client Activity
package x.y.z;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
public class ClientActivity extends AppCompatActivity
{
private final String TAG = "ClientActivity";
private ServiceConnection mMyServiceConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.d(TAG, "ServiceConnection onServiceConnected");
}
#Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG, "ServiceConnection onServiceDisconnected");
}
};
private boolean isServiceRunning(String className) {
ActivityManager manager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo serviceInfo : manager.getRunningServices(Integer.MAX_VALUE)) {
if (className.equals(serviceInfo.service.getClassName())) {
return true;
}
}
return false;
}
private void bindMyService() {
Intent intent = new Intent("a.b.c.MY_INTENT");
intent.setPackage("a.b.c");
bindService(intent, mMyServiceConnection, Context.BIND_AUTO_CREATE);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_client);
if (isServiceRunning("a.b.c.MyService")) {
bindMyService();
}
else {
Log.e(TAG, "Service is not running");
}
}
}
The "isServiceRunning" function returns true so I know that a.b.c.MyService is running. The bindService function seems to succeed (no errors in Logcat) but the onServiceConnected callback is never executed.
How do I bind to a.b.c.Myservice from x.y.z.ClientActivity in Android target SDK 24?
Thanks!
I see at least 2 problems here:
You are using an "implicit" Intent in the call to bindService() (ie: you haven't specified the component explicitly). This should throw an exception if you are targeting API 21 or higher. You should use an "explicit" Intent (ie: specify the package name and class name of the Service).
Your Service returns null from onBind(). This means that clients will not be able to bind to the Service, because you aren't returning an IBinder that the client can then use to call methods on the Service. Try to find a how-to or some example code for how to use bound Services.
im trying to start an application on device boot.
my code is as follow
1- firstly the main class which contain a background thread (asynctask) that send data to a mysql database.
2- Service class
package com.seven.ex.helper;
import com.seven.ex.AndroidGPSTrackingActivity;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class service extends Service {
private static final String TAG = "MyService";
#Override
public IBinder onBind(Intent intent) {
return null;
}
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)
{
Intent intents = new Intent(getBaseContext(),AndroidGPSTrackingActivity.class);
intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intents);
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
}
}
3- BootUpReceiver class
package com.seven.ex.helper;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class BootUpReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context arg0, Intent arg1) {
Intent intent = new Intent(arg0,service.class);
arg0.startService(intent);
Log.i("Autostart", "started");
}
}
4- the android manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gpstracking"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="com.seven.gpstracking.AndroidGPSTrackingActivity"
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=".service"
android:label="#string/app_name"
>
</service>
<receiver android:enabled="true" android:name=".BootUpReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
The main method is working correctly before i tried to start it on boot.
Also the app still working after i ignore the android error on boot (The App Closed)
in the log cat am getting a class not found exception
can you please help ?
Your Service won't work like it is at the moment. You will have to move the stuff from onStart() to onStartCommand() and then return whether the service is sticky or not. The problem is that the onStart()-method has a good chance of not getting called at all (as it is deprectaed by now).
#Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
//do your stuff here
return Service.START_NOT_STICKY;
}
In your manifest you have assigned package: com.example.gpstracking. When u then define .BootUpReceiver. The system should expect to locate the class at com.example.gpstracking.BootUpReceiver.
Please try and change the package .BootUpReceiver to the full path com.seven.ex.helper.BootUpReceiver. The same goes for AndroidGPSTrackingActivity as this should be com.seven.ex.AndroidGPSTrackingActivity as far as I can tell.
Hello i am writing an application, which is when the phone reboot, the service will auto start instead of click on the application.
Here is my code for
BootCompleteReceiver.java
package com.example.newbootservice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootCompleteReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, MsgPushService.class);
context.startService(service);
}
}
MsgPushService.java
package com.example.newbootservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class MsgPushService extends Service {
#Override
public void onCreate() {
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
return Service.START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroy", Toast.LENGTH_LONG).show();
}
#Override
public IBinder onBind(Intent arg0) {
return null;
}
}
MainActivity.java (not sure whether i need this)
package com.example.newbootservice;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startService(new Intent(getBaseContext(), MsgPushService.class));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.newbootservice"
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" >
<service android:name=".MsgPushService"/>
<receiver android:name=".BootCompleteReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I want the service to be started automatically after reboot instead of starting it manually.
You forgot about the permissions
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Though all the above answers were correct, however from Android Oreo they put some limitation on Background Services.
Check Background Execution Limits to know more about background limits in Android O.
You can't start a Service directly from BroadCastReceiver when the application is in the background.
However, you can start a foreground service from the receiver by calling startForegroundService() or use JobIntentService as there is no such limitation with JobIntentService.
Use this code on your Broadcast Receiver class:
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent service = new Intent(context, MsgPushService.class);
context.startService(service);
}
also try these permissions,it may help you .ifyou are using htc phones then these permissions are required
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
How to start a service in android using nested class when mobile is switched ON?
I have the package contains nested class.
package Name
com.android
MainActivity
BroadCastReceiver
I am trying to reboot my BroadCast receiver. But it doesn't work (getting Failed). I don't know if the reason is, because of any problems for nested classes.
<receiver android:name="ConnectionReceiver"></receiver>
<receiver android:name="com.android.MainActivity .BroadCastReceiver ">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="onchip.automobile.caraccessory1"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<receiver
android:name=".Onchip_BroadcastReceiver">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
<service
android:name=".Onchip_BackgroundService">
<intent-filter >
<action android:name="onchip.automobile.caraccessory1.BACKGROUND_SERVICE" />
</intent-filter>
</service>
</application>
</manifest>
Onchip_BroadcastReceiver.java
package onchip.automobile.caraccessory1;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class Onchip_BroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent serviceIntent = new Intent("onchip.automobile.caraccessory1.BACKGROUND_SERVICE");
context.startService(serviceIntent);
}
}
Onchip_BackgroundService.java
package onchip.automobile.caraccessory1;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Toast;
public class Onchip_BackgroundService extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
Toast.makeText(this, "Onchip_BackgroundService Created", Toast.LENGTH_LONG).show();
}
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Toast.makeText(this, "Onchip_BackgroundService Started", Toast.LENGTH_LONG).show();
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Onchip_BackgroundService Destroyed", Toast.LENGTH_LONG).show();
}
}
R u having BroadcastReceiver as nested class. if yes dont do this.
1)if you have receiver as nested class, register it in activity dynamically, do not register in manifest
2) if you are registering it in manifest , make separate class for Receiver.
i have a problem with a simple service. the service should run "automatically", as i already know if boot completet.
i've found a simple example and my code is the same but it doesnt work, there is nothing in the log. here is the example [Start Service at Boot][1]
package com.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MainService extends Service
{
public IBinder onBind(Intent intent)
{
return null;
}
#Override
public void onCreate()
{
Log.v("StartServiceAtBoot", "StartAtBootService Created");
}
#Override
public void onStart(Intent intent, int startId)
{
Log.v("StartServiceAtBoot", "StartAtBootService -- onStart()");
}
#Override
public void onDestroy()
{
Log.v("StartServiceAtBoot", "StartAtBootService Destroyed");
}
}
[1]: http://www.androidcompetencycenter.com/2009/06/start-service-at-boot/
here the braodcast receiver for the Boot-completed Action:
package com.service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class MainServiceReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
{
Log.i("Test", "Test");
Intent i = new Intent();
i.setAction("com.service.MainService");
context.startService(i);
}
}
}
and here the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.service"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses- permission>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<service android:name="com.service.MainService">
<intent-filter>
<action android:name="android.action.intent.BOOT_COMPLETED">
</action>
</intent-filter>
</service>
<receiver android:name="com.service.MainServiceReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
<category android:name="android.intent.category.HOME">
</category>
</intent-filter>
</receiver>
</application>
in the android error log there is always this error:
9009 Applications Change Receiver updating1 application packageName = com.service....
please help me i think its a little thing but I dont see the mistake.
Thank you
In your code:
Intent i = new Intent();
i.setAction("com.service.MainService");
context.startService(i);
But at in manifest you have not declared intent-filter to receive this action.
You only need startSerice by call:
startService(new Intent(MainServiceReceiver .this,MainService.class));
If you have any problem, please add my yahoo or skype :fsoft_duonghv