I have made a simple service in android studio which should start as soon as I boot the phone. It should display atleast a TOAST Message. I am using Redmi note 4 as emulator and the service is not starting when I boot or reboot the phone. I have set the app to autostart also in settings.
Android Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dilip3.myapplication" >
<!-- Permission for starting app on boot -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Service required starting app on boot -->
<service android:name=".MyService" android:label="My Service">
<intent-filter>
<action android:name="com.myapp.MyService" />
</intent-filter>
</service>
<receiver
android:enabled="true"
android:name=".BootService"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.REBOOT"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
</application>
</manifest>
BootService.java
package com.example.dilip3.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class BootService extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Toast.makeText(context, "Boot Completed", Toast.LENGTH_SHORT).show();
Intent serviceIntent = new Intent(context, MyService.class);
context.startActivity(serviceIntent);
}
}
}
MyService.java
package com.example.dilip3.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 {
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "service starting", Toast.LENGTH_LONG).show();
return super.onStartCommand(intent,flags,startId);
}
}
MainActivity.java has no changes.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
You need to start a service, not an activity.
From
context.startActivity(serviceIntent);
To
context.startService(serviceIntent);
Related
I'm a newbie to android programming
I am trying to read incoming SMS and the SMS sender number and display it in my app.
As soon as my phone receives an SMS the app closes
Is there a simple Solution, Should I not be using two classes in the same activity
package com.example.readnewmsg;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.provider.Telephony;
import android.support.v7.app.AppCompatActivity;
import android.telephony.SmsMessage;
import com.example.readnewmsg.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
}
public class SmsListner extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Telephony.Sms.Intents.SMS_RECEIVED_ACTION)) {
String smsSender = "";
String smsBody = "";
for (SmsMessage smsMessage : Telephony.Sms.Intents.getMessagesFromIntent(intent)) {
smsBody += smsMessage.getMessageBody();
smsSender += smsMessage.getOriginatingAddress();
binding.MsgText.setText(smsBody);
binding.NoView.setText(smsSender);
}
}
}
}
}
I have Added these permissions in My manifest file
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
And also these intent filters
<application
android:allowBackup="true"
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>
<receiver android:name=".MainActivity$SmsListner">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
I have an android foreground service that is supposed to start running when the device boots up. At the boot the banner rolls by signaling that the service has started, however the banner does not stay in the notification bar after the initial notification runs buy. Additionally the service does not continue to run.Below is my manifest, broadcast receiver, and service.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.copyright.Going_Plaid"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
/>
<application
android:allowBackup="true" android:label="Going_Plaid"
android:icon="#mipmap/ic_launcher" android:theme="#style/AppTheme">
<receiver android:name="com.example.copyright.Going_Plaid.autostart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<activity
android:name="com.example.copyright.Going_Plaid.MainActivity"
android:label="Going_Plaid">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<service
android:name="com.example.copyright.Going_Plaid.Scan"
android:exported="false"
android:enabled="true"
/>
</application>
autostart.java:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class autostart extends BroadcastReceiver {
public void onReceive(Context arg0, Intent arg1){
Intent intent = new Intent(arg0, Scan.class);
arg0.startService(intent);
Log.v("autostart", "service should be started");
}
}
Scan.java:
public IBinder onBind(Intent mServiceIntent){return null;}
#Override
public void onCreate(){
startInForeground();
notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
#Override
public int onStartCommand( Intent mServiceIntent,int flags, int startId) {
executorService.execute(new Runnable() {
Most of the code has been redacted because I did not think it was relevant. The service does run fine when started through the UI but runs into issues when starting from boot without the user manually starting it. If you need any other information I would be happy to add it. Thank you in advance for your assistance.
I am trying to create an Android app in Android Studio.
The app is mainly a background service to send some information to the server around every 15 seconds or so.
I have created an empty activity, boot receiver and a service class, but it doesn't work.
Could somebody help me, and explain why it doesn't work.
The relevant files:
MainActivity.java
package nl.robinvandervliet.robinsapp.app;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
public class MainActivity extends ActionBarActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
finish();
}
}
BootReceiver.java
package nl.robinvandervliet.robinsapp.app;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
context.startService(new Intent(context, RobinsService.class));
}
}
RobinsService.java
package nl.robinvandervliet.robinsapp.app;
import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class RobinsService extends Service
{
private Runnable runnable = new Runnable()
{
#Override
public void run()
{
//Opening connection to the server.
while (true)
{
//Sending some data to the server.
//Sleeping for around 15 seconds.
}
//Dead code, should never reach this place.
}
};
public IBinder onBind(Intent intent)
{
return null;
}
#Override
public void onCreate()
{
new Thread(runnable).start();
Notification notification = new Notification.Builder(getApplicationContext()).setContentTitle("MyService is running!").setContentTitle("(more information later)").build();
startForeground(1, notification);
Toast.makeText(getApplicationContext(), "Service created!", Toast.LENGTH_LONG).show();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="nl.robinvandervliet.robinsapp.app" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="nl.robinvandervliet.robinsapp.app.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="nl.robinvandervliet.robinsapp.app.BootReceiver" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name="nl.robinvandervliet.robinsapp.app.RobinsService" android:exported="false" />
</application>
<uses-permission android:name="android.permission.BATTERY_STATS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
</manifest>
Thanks, Robin.
Since you haven't implemented some parts yet I assume you just want it to run when you boot your device. Maybe take a look here. Adding an extra action in the manifest might solve your problem.
My app successfully installs on my AVD, I can confirm it was installed by seeing it in the app manager, but I can never access it because I can not see it in the app menu
In the console I always get the messages
[2011-12-27 10:39:28 - WhosurSensei] No Launcher activity found!
[2011-12-27 10:39:28 - WhosurSensei] The launch will only sync the
application package on the device!
my opening java page
package com.thepackage.WhosurSensei;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class WhosurSenseiActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button Button1 = (Button) findViewById(R.id.firstpagebutton);
Button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(WhosurSenseiActivity.this, MainMenu.class));
}
});
}
#Override
protected void onPause() {
super.onPause();
}
}
my androidmanifest page
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/splash"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".WhosurSenseiActivity" >
<intent-filter >
<action android:name="com.thepackage.WhosurSensei.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="#string/app_name"
android:name=".MainMenu" >
</activity>
</application>
</manifest>
Apparently the tutorial I was reading told me wrong and I needed this in the manifest:
<action android:name="android.intent.action.MAIN" />
How to automatically start a service in Android 3.x, the test tabblet is a Samsung Galaxy 10.1. My code works on a noname tabblet with android 2.2.1 The code works nor in android emulator with the android version 3.x
Code:
StartAtBootService.java
package test.autostart;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class StartAtBootService extends Service
{
public IBinder onBind(Intent intent)
{
return null;
}
#Override
public void onCreate()
{
Log.v("StartServiceAtBoot", "onCreate");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.v("StartServiceAtBoot", "onStartCommand()");
return START_STICKY;
}
#Override
public void onDestroy()
{
Log.v("StartServiceAtBoot", "onDestroy");
}
}
StartAtBootServiceReciver.java
package test.autostart;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class StartAtBootServiceReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent();
i.setAction("test.autostart.StartAtBootService");
context.startService(i);
}
}
}
Manifest
<application android:icon="#drawable/icon" android:label="#string/app_name">
<service android:name="StartAtBootService">
<intent-filter>
<action android:name="test.autostart.StartAtBootService">
</action>
</intent-filter>
</service>
<receiver android:name="StartAtBootServiceReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
<category android:name="android.intent.category.HOME">
</category>
</intent-filter>
</receiver>
</application>
</manifest>
It was a SD-card issue, Eclipse install new apps on the SD-card by default on my Samsung Galaxy 10.1. To fix the issue I needed to add android:installLocation="internalOnly" in the manifest.
The new Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.autostart"
android:versionCode="1"
android:versionName="1.0" android:installLocation="internalOnly">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<service android:name="StartAtBootService">
<intent-filter>
<action android:name="test.autostart.StartAtBootService">
</action>
</intent-filter>
</service>
<receiver android:name="StartAtBootServiceReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED">
</action>
<category android:name="android.intent.category.HOME">
</category>
</intent-filter>
</receiver>
</application>
</manifest>
I hope this will help somone in the futre.
Make sure your application is not on SD. I your application needs to run on boot time, then do not place it on SD.