How to solve foreclose problem in start up my application? - android

I am developing an android application,I want to my application automatically invoked,when I switch on my device.so i used this permission <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> in my application's manifest.xml file and i used service , activity and register Broadcast receiver in manifest.xml file.but I got foreclose error appear in when will i start up my device.How solve this proplem.
My Receiver code given bellow
context.startActivity(new Intent(context,ServicesDemo.class));
//In my mainfest.xml for myReceiver
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter> </receiver>
I got this exception java.lang.RuntimeException: Unable to start receiver com.servicedemo.MyReceiver: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

I am developing an android application,I want to my application automatically invoked,when I switch on my device
Users generally hate that with the fiery passion of a thousand suns. Please make this configurable and disabled by default.
Also, bear in mind that if too many developers abuse this, the core Android team is likely to prevent you from starting an activity at boot time.
How solve this proplem
Do what the error message told you to do: add the FLAG_ACTIVITY_NEW_TASK flag to your Intent.

Related

POC - Start Service on boot without an Activity - Android 4+

This question may seem a trivial but I've been struggling with it.
I'm trying to start a service on boot and everything works fine if I start it at least once from the mainActivity (launch activity), something like:
AndroidManifest.xml
...
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
...
<activity
android:name="com.example.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="com.example.bootReceiver" android:enabled="true" android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
<service android:name="com.example.someService" android:enabled="true" android:exported="false"/>
...
bootReceiver.java
...
startService(new Intent(getApplicationContext(), com.example.someService.class));
...
mainActivity.java
...
startService(new Intent(getApplicationContext(), com.example.someService.class));
...
With the above code I'm able to run the service after every boot without any problems.
As a POC, I'm trying to start a service without any activity, or at least without the mainActivity, just by declaring the service on the AndroidManifest.xml or by creating new activity (invisible?) that is launched at the same time as the default launcher activity.
As far a I know, this isn't possible on android 3+ (4+?) due to security reasons.
Is there any way to achieve this ?
Can I start 2 activities from the AndroiManifest.xml when the user launches the app?
I'm sorry if my question isn't 100% clear, I've tried to explain it the best I could, if you cannot understand it, please leave a comment bellow. Tks.
As far a I know, this isn't possible on android 3+ (4+?) due to security reasons.
Android 3.1, actually, to help prevent drive-by malware.
Is there any way to achieve this ?
Something has to use an explicit Intent to invoke some form of IPC on one of your components, to move the app out of the so-called "stopped state" that is preventing you from receiving the broadcast. So, something needs to either:
start one of your activities via an explicit Intent, or
start one of your services via an explicit Intent, or
send a broadcast to one of your receivers via an explicit Intent
(I don't know if trying to connect to a ContentProvider will work, though arguably it should)
The key is the explicit Intent. That's why invoking an activity from a home screen launcher works, because the Intent used to start your activity will be an explicit one.
However, in the absence of such an activity, you would need to find something else that would use an explicit Intent to invoke one of your components. Certain specialized services (e.g., an input method) probably get invoked with an explicit Intent if and when the user activates that app's capability via the system Settings app. If you're a plugin for some other app, that other app might use an explicit Intent to work with one of your components. You can ask the user to install the Android SDK, learn how to use the command line, and invoke an adb shell am command to start one of your components. And that's about all I can think of off the top of my head. None are exactly general purpose solutions.
or by creating new activity (invisible?) that is launched at the same time as the default launcher activity
I have no idea what you think that would achieve. If the user starts up your launcher activity, you're already out of the stopped state and will receive broadcasts as normal.

How to start my application on phone shutdown event

I am making an android app, in which the app must launch just before the phone is shutting down. Is that possible?
Here i am not talking about encountering phone shutdown event when my app is running.
You will need this permission in your manifest file
<uses-permission android:name="android.permission.DEVICE_POWER" />
And you will listen to the intent like this
<receiver android:name=".ShutdownReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
</intent-filter>
</receiver>
You can look at the links here for some description and here for official documentation.
I don't know the exact application where you want to use it, but you can have a background service running with the receiver for this intent.

Start Android Service with no Activity using an intent

i created an android application with no activity. I want to start a service using a system intent like BOOT_COMPLETED. I use the following receiver:
<receiver android:name=".autostart" >
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.SCREEN_ON" />
</intent-filter>
I got the problem, that the intent is not received when power is connected/disconnect or boot completed. Is an Application with no Activity even in stopped mode after install? How can I start the service? UI is not possible because the application has no activity...
Is an Application with no Activity even in stopped mode after install?
Yes.
UI is not possible because the applicatio has no activity...
Then add one. You need one anyway, to present your license agreement, your online help, your configuration for this background processing, and so forth. And, since your app will not run until the user launches this activity, you need to for that reason as well.
Every Android application needs to be launched at least once after installation and only then it will receive any intents from system. This means an application without any gui will not work in your case.
Many applications include only "about" activity, which is a common way to deal with that.
Please see:
http://commonsware.com/blog/2011/07/13/boot-completed-regression-confirmed.html
http://developer.android.com/about/versions/android-3.1.html#launchcontrols

Restrict Broadcast Receiver to application

I am working on broadcast receiver and stuck in a problem.
I am receiving a broadcast receiver in Manifest file.
<receiver class=".MyClass" android:name=".MyClass">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.ConnectivityManager.CONNECTIVITY_ACTION" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
this is working fine and it is calling MyClass whenever there is change in connectivity.
Now the problem is whenever my application is not running still this class will receive broadcast receiver. I want it to receive whenever the application is running.
I tried it by extending BroadcastReceiver registering and unregistering broadcast in that class file and it works. But i want to achieve the same by Manifest file.
My problem will solve if it is not receiving anything when application is not opened.
What you are talking is not possible. The whole purpose of having the intent filter in the manifest is being able to receive the intent whether or not your application is running. The only way to do what you want to is by registering/unregistering the receiver from the code [registerReceiver]
The question was asked a long time ago but in case some one landed on this page while searching, It is possible to register and unregister broadcast receiver from code instead of doing that from manifest file. (Checking the Networking Connectivity using BroadcastReceiver in Android)
You said "My problem will solve if it is not receiving anything when application is not opened".
Here how I understand your question and appropriate answer.
android:enabled
Whether or not the broadcast receiver can be instantiated by the system — "true" if it can be, and "false" if not. The default value is "true".
If you want to enable your receiver at runtime, you can set the state to disabled initially. You can do so in the manifest file:
<receiver
android:name=".YourReceiver"
android:enabled="false" >
<!-- your intent filter -->
</receiver>
Source:
http://developer.android.com/guide/topics/manifest/receiver-element.html#enabled
http://www.grokkingandroid.com/enabling-and-disabling-broadcastreceivers/

I want my Android app to know when another application has been started

I would like my app to do something when another application is opened.
The current approach I have taken is to create a broadcast receiver that monitors all
android.intent.action.MAIN
events, but either I am not doing it correctly or the way I am going about it is incorrect. The section of the manifest looks like this:
<receiver android:name=".GetApp">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
I included the launcher category just as a test. GetApp currently is only set to make a log entry when called.
If anyone has any experience doing something like this your help would be greatly appreciated!
After doing some more digging in the Android documentation I found that a broadcast receiver would not pick up on an app starting because it goes through createActivity(). Calls to createActivity() are not considered broadcasts and therefore cannot be received by broadcast receiver.

Categories

Resources