I am new to Eclipse and Java development and I am trying to start an activity on boot. I've read multiple threads discussing this topic and while I have managed to start the application on boot, it crashes.
This is my code:
AndroidManifest.xml :
<!--
Below the <manifest> opening tag:
-->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<!--
Inside the <application> tag:
-->
<receiver android:name="com.example.Autostart">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name="com.example.service" android:enabled="true" />
Autostart.java :
package com.example;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class Autostart extends BroadcastReceiver{
#Override
public void onReceive(Context arg0, Intent arg1)
{
Intent intent = new Intent(arg0, service.class);
arg0.startService(intent);
}
}
service.java :
package com.example;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class service extends Service{
#Override
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public void onStart(Intent intent, int startid)
{
Intent intents = new Intent(getBaseContext(),checker.class);
intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intents);
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
}
}
My checker class has an OnCreate function which alters some settings values.
The only thing that I see when I start my phone is that "YourAppName has stopped working", which means that the application has crashed. I do not see any Toast message.
When I normally open my application and I read the settings that ought to be written on startup, nothing is there.
Change like this in your manifest and try it:
<receiver android:enabled="true" android:name=".Autostart"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
The problem was that I hadn't declared my activity to the manifest file.
Related
Im trying to make an app that getting all notifications and do the task. But my problem is NotifycationListener service starting automaticly even i didnt call the start service. Its starting as soon as I allow the app notification access on my phone. So service starting somehow and I cant stop it. Im already try simple services they working correctly. But this one with the NotificationListener is really painful. I just want to start and stop this service by my command.
Service start by itself when I allow this (screenshot)
MainActivity
package com.example.alperen.nservice2;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button startB,stopB;
Intent intent;
int count=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startB=(Button)findViewById(R.id.button);
stopB=(Button)findViewById(R.id.button2);
startB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
intent = new Intent(MainActivity.this,MyService.class);
startService(intent);
}
});
stopB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
stopService(intent);
}
});
}
}
MyService Class
package com.example.alperen.nservice2;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.service.notification.NotificationListenerService;
import android.service.notification.StatusBarNotification;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;
public class MyService extends NotificationListenerService{
#Override
public void onCreate() {
super.onCreate();
System.out.println("********* SERVICE STARTED ***********");
}
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
System.out.println("***** notification *****");
String pack,title,text;
Bundle extras;
try {
pack = sbn.getPackageName();
extras = sbn.getNotification().extras;
title = extras.getString("android.title").toString();
text = extras.getCharSequence("android.text").toString();
}catch (Exception e)
{
System.out.println("**** HATA NOTIFYSERVICE CLASS ****");
pack="empty1";
title="empty1";
text="empty1";
System.out.println("**** "+pack+" ****");
}
Log.i("Package",pack);
Log.i("Title",title);
Log.i("Text",text);
Toast.makeText(this,"title: "+title+" text: "+text,Toast.LENGTH_LONG).show();
}
#Override
public void onDestroy() {
System.out.println("***** destroyed *****");
super.onDestroy();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.alperen.nservice2">
<uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
tools:ignore="ProtectedPermissions" />
<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 android:name=".MyService"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
></service>
</application>
And the tricky part is in the Manifest. When I delete intent-filter part and run the app. It doesn't want to notification access anymore and it doesn't start by itself. I can start and stop the service from the MainActivity with buttons. But this time the app not getting the notification.
// just delete this lines
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
This is an intended behavior.
When you grant notification access permission (or restart the phone) then SYSTEM is binding to your NotificationListenerService in order to send notification data.
If you override your services onBind method and log it, you will see it's being called.
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);
I use service in my app for lock statusbar.
This service is sticky and run on boot start
But my service start after 2-5 second and actived (on boot start).
Can i decrease this time?
public class BroadcastReceiverOnBootComplete extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {
Intent serviceIntent = new Intent(context, AndroidServiceStartOnBoot.class);
context.startService(serviceIntent);
}
}
}
Service
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class AndroidServiceStartOnBoot extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
// here you can add whatever you want this service to do
}
}
AndroidManifest.xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
<receiver
android:name=".BroadcastReceiverOnBootComplete"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
<service android:name=".AndroidServiceStartOnBoot"></service>
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.