Draw over other app service reopens itself after closing - android

I would like to apologize if this is somehow a repeated question that has been answered. I have tried the solutions I was able to find here and still not able to do the thing I need.
I am still trying to learn programming with android system and currently trying to do something like the chat bubble that draws over other app. From learning on this site, I was able to do it by opening an intent, but I have not been able to find a way to close the app completely--even after swiping it to close after pressing the home key for a short period of time, the app will automatically reopens itself. Let me try to explain it better with what I am doing.
In manifest:
I have
and these:
<activity
android:name=".Play"
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="com.example.play.MainClass"
android:exported="true" />
In Play.java
public class Play extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent svc = new Intent(this, MainClass.class);
startService(svc);
}
#Override
public void onDestroy() {
super.onDestroy();
}
}
In MainClass.java
I am using this
public class MainClass extends Service {
.......
}
as well as using
private WindowManager wm;
and do the codes using the wm to display the chat bubble i need.
I have been trying to search how to close android app programmatically and phrases like that and I found that many answers include doing:
System.exit(0);
or
finish();
or
Process.killProcess(int pid)
But then again, I am not able to close my app completely--it runs again by itself after around 10-20seconds. I am thinking it is because I am using 'Service' so I am not able to close the app completely.
I am looking forward to any of your kind reply and patience.
I apologize in advance for if this is still answered somewhere that I was not able to find it.
Thank you very much.

Related

Waking up Android for a call

I am making a VOIP call app. When a call comes in, it should open the app, even if the app is closed or never opened. We also need the call notifications to come in from a server.
I know apps like WhatsApp and Facebook Messenger do this, but how?
We are using a design similar to follows: Build a Calling App
We have tried using Firebase Cloud Messaging as recommended by Android documentation, and while there is moderate success, it does not work when the app is closed.
We are considering using a Sync Adapter or WorkManager next, but it takes quite a bit of time to prototype, and I'd prefer to ask if anyone has any success or if there are existing plugins for this.
As I'm aware, there are also going to be restrictions on Android 10. It says to use time-sensitive notifications, but these will still need to be triggered from a server somehow.
In your calling activity add this code. It will turn your screen on.
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
this.setTurnScreenOn(true);
} else {
final Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}
and in your activity manifest, define intent-filter as CALL
<activity
android:name=".activities.CallActivity"
android:label="#string/app_name"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan|stateHidden">
<intent-filter>
<action android:name="android.intent.action.CALL" />
</intent-filter>
</activity>
When you receive notification from Firebase in onMessageReceived(RemoteMessage remoteMessage), open your activity
openActivity(CallActivity.class);
public void openActivity(Class<?> tClass) {
Intent i = new Intent(this, tClass);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}

Must every activity have a layout?

I'm just getting started with Android and was reading up BroadcastReceiver. Since the MainActivity was being used only to get the alarm time in seconds, it got me thinking whether layout XML files are must for every activity in Android. I mean, is it possible to have an app that when launched, shows no view, but successfully sets up a receiver?
The answer is yes it's possible. Activities don't have to have a UI. It's mentioned in the documentation, e.g.:
An activity is a single, focused thing that the user can do. Almost
all activities interact with the user [...]
(see http://developer.android.com/reference/android/app/Activity.html)
Related SO question: https://stackoverflow.com/a/12817384/534471
To e.g. display a Toast from an Activity without layout you would define the activity in your manifest like so:
<activity
android:name=".MainActivity"
android:theme="#android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
The code would look like this:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(this, "I'm alive", Toast.LENGTH_LONG).show();
finish();
}
}
You can implement an Activity without a UI. In the manifest you can specify android:theme="#android:style/Theme.NoDisplay". Take a look at this
You can also implement a Service which does not have any UI so you do not need layout inflation. Service just runs in background and shows no views.
Take a look at Android Training and API Guide to learn more about Services

Issues with Translucent Theme

I have an app that has two activities.
The first one is presented with a single button that opens the second one.
Here is the Manifiest definition for the first one:
<activity
android:name="com.example.buttonexample.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Second activity:
<activity
android:name="com.example.buttonexample.MainActivity2"
android:label="#string/title_activity_main_activity2" android:theme="#android:style/Theme.Translucent">
</activity>
Here is how I launch the second activity (via OnClickListener for a button on the first activity):
public void startSecondActivityClick(View v) {
Intent startActivity2 = new Intent(this, MainActivity2.class);
startActivity(startActivity2);
}
This works fine, however when I background the app by hitting home and the foreground the app. I'm noticing that the first activity is continually creating/destroying itself. I verified this by putting some code in the onDestory method to increment a static int:
private static int count = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
count++;
}
protected void onDestroy() {
super.onDestroy();
Log.i("MainActivity", String.format("Destroyed, %d", count));
}
I've also noticed that removing the translucent theme seems to fix this. My question is is there a way to translucent or something similar but also have it not restart? Also, I'm curious why this happens at all. I'm testing this on 4.0.1 ICS on a galaxy SIII.
Ok after some digging I was able to figure out why this is happening. Someone had turned on one of the developer options, "do not keep activities.". After turning this off this stopped happening. I suspect this wouldn't happen in production too often as most people probably don't have that setting on. You can find this under settings -> "developer options" on most phones.

Need to switch my app to background after a phone restart or power on

I need my android app to be in background mode after a phone restart/power on.
Currently I am using the following code, so that my app successfully gets launched after a phone restart/power on.
AndroidManifest.xml:
<receiver android:enabled="true" android:name="my_package.BootUpReceiver" 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>
BootUpReceiver.java:
public class BootUpReceiver extends BroadcastReceiver
{
private static SharedPreferences aSharedSettings;
#Override
public void onReceive(Context context, Intent intent)
{
aSharedSettings = context.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
boolean isUserLoggedIn = aSharedSettings.getBoolean(Key.AUTHENTICATED, false);
if(isUserLoggedIn)
{
Intent aServiceIntent = new Intent(context, MyHomeView.class);
aServiceIntent.addCategory(Intent.CATEGORY_HOME);
aServiceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(aServiceIntent);
}
}
}
As I said above, my app successfully gets launched after a phone restart/power on.
However, after the phone restart/power on, my app was in foreground mode. But I need my app to be in background mode.
Can anyone please say, how to make an app to be in background mode after a phone restart or power on.
I even tried by changing the intent category to
<category android:name="android.intent.category.HOME" />
But no use in it. Can anyone please help me?
Thanks.
I need my app to be just running in background after the phone restart, so that users can select from the minimized app
I think your approach is wrong. All you are trying to do now is to add icon of your app to recent apps list. Your app won't run in background and I think you don't really want it. Am I right?
Recent apps list managed by android and IMHO forcing your app to be in recent apps list is not a very good idea. User will start you app when he wants from launcher or icon on his desktop.
If your broadcast receiver is working fine and app is starting successfully then you can use the below code in your MyHomeView activity's onCreate method to go to the home screen.
Trick is to click HOME button programmatically when app starts.
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
you can pass some variable from the BroadcastReceiver to differentiate a normal request and BroadcastReceiver's request to make the above code conditional.
But if you want to execute it always in background then it would be better to use Service.
It is recommended to change your code to the service to run it in background.
The suggestion which Leonidos replied is correct.
However, Just a workaround for this:
In my BootUpReceiver, I had a seperate boolean flag for this! (Its a bad way. but just a workaround)
SharedPreferences.Editor aPrefEditor = aSharedSettings.edit();
aPrefEditor.putBoolean(Key.IS_DEVICE_RESTARTED, true);
aPrefEditor.commit();
In Oncreate method of MyHomeView:
boolean isDeviceRestarted = aSharedSettings.getBoolean(Key.IS_DEVICE_RESTARTED, false);
if(isDeviceRestarted)
{
SharedPreferences.Editor aPrefEditor = aSharedSettings.edit();
aPrefEditor.putBoolean(MamaBearKey.IS_DEVICE_RESTARTED, false);
aPrefEditor.commit();
moveTaskToBack(true);
}
Thanks

Android: I want to create a new file in sdcard when the apk is installing

I want to create a new file in sdcard when the apk is installing...
How or where should I code?
Thanks in advance!
You definitely can not do that. That would introduce all kinds of security concerns. As Ken Y -N said in his answer, the best thing to do would be to detect the first time that your app is opened, and do something there.
Here's an activity class to do this:
public class StartupChoiceActivity extends Activity {
private static final String TAG = StartupChoiceActivity.class.getSimpleName();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences mPrefs = getSharedPreferences("prefs", Context.MODE_PRIVATE);
if(!mPrefs.getBoolean("has_started_before", false)) {
// Do what ever you want to do the first time the app is run
} else {
//Remember our choice for next time
mPrefs.edit().putBoolean("has_started_before", true).commit();
Log.i(TAG, "We've already started the app at least once");
}
//Do what ever we want to do on a normal startup. This is pretty much always mean starting a new activity
startActivity(new Intent(this, MyNormalFirstActivity.class);
finish();
}
}
The only other thing you need to do is make sure this activity is set as your 'Startup' activity in the AndroidManifest.xml. You can do this by putting this code inside your application tag:
<activity android:name=".StartupChoiceActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You cannot do that; such a feature would be a good way of getting trojans to propagate, for instance.
Just check for the first run of your program and do the initialisation there.

Categories

Resources