Android StartAcitivty() with intent - android

I'm making a SIP application, and I've got pretty much everything working that i want to except one thing. I cant get the activity to launch when a SIP call occurs.
I'm working with a SIP library that starts a 'phone' service which deals with the handling of incoming calls etc, and when i create an instance of the 'phone' I then register listeners which detail what i want to happen. So to handle incoming calls i register an 'OnIncommingCall' listener.
If the app isn't currently open at the time i want it to launch the app. so my listener is:
thisPhone.setIncomingCallListener(new OnIncomingCallListener()
{
public void OnIncomingCall(String remoteContact, int accountId)
{
if(MainActivity.this.getIsOpen())
{
MainActivity.this.setIsCallIncomming(true);
MainActivity.this.setCurrentCaller(remoteContact);
MainActivity.this.setMainUIEles();
}
else
{
Intent i = new Intent(getApplicationContext(), MainActivity.class);
i.putExtra("isCallIncomming", true);
i.putExtra("currentCaller", remoteContact);
i.putExtra("isRegistered", true);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
}
}); //incoming call listener
However when the call comes, it rings but the app wont open, but if i open the app from the launcher whilst the call is still ringing then it will open and know the call is there.
Is the problem because I'm trying to start the Activity which defined the listener?
I've tried all sorts of flags and combinations (SINGLE_TOP etc) can i can't get it to work.
Any help would be appreciated!
DJOodlen

Well this was a silly mistake on my part.
After putting in some toasts so I could see where in the code each bit was going I realised that getIsOpen() never returned false. That's because it wasn't being called on the onPause() method as wells as the onDestroy() method...
Ooops!

Related

How to get a callback from Android system to the app?

I'm making an app in which you can chat and call with other contacts. But in case of calling, I've designed the app in such a way that after typing the number and clicking on the call icon, it takes you to native calls app for calling and updates the call log in my current app.
For this process, this is the code I've written:
if (nativeCall(mobileNumber)) {
Intent intent = new Intent(Intent.ACTION_CALL).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("tel:" + mobileNumber));
if (((BaseActivity) context).isNetworkOk()) {
addToUserCallLogs(context, DateUtils.convertTimestampToDate(), contactUri, "Out", System.currentTimeMillis());
}
context.startActivity(intent);
return true;
}
You can see that I'm putting mobile number into the intent and starting it. And I'm using addToUserCallLogs() function to show it in my app's call logs.
This works fine usually, but in the issue is in the following case.
When the user has multiple calling applications(For eg, the user has installed application named SMARTalk. Now he has native caller app and SMARTalk app to call from), in that case the Android system gives options to chose from like this:
Now, if he choses from one of them, even in that case there is no issue. Say he didn't chose any of those and clicked on the other part of the screen. Then this options bar will be closed. Since all this is happening after starting the intent, this call will be added in the call logs of the app from the function addToUserCallLogs(). But I don't want the call to be shown in the call Logs because I haven't done any call.
But according to the code I've written, before starting the intent, I'm adding into my app's call logs database. Is there a way the information of whether the call has happened or not can be sent back from the system to the app?
Or a way to get these options to be shown manually from the app?
Please comment if you need any more explanation.
I guess no way to receive the callback information because ACTION_CALL does not return a result. You can see the output is nothing from docs even you use startActivityForResult

ObservableObject updateValue doesn't reach the right instance after startActivity

I have a basic Activity which mainly allows the user to change settings and save them. I also have a BroadcastReceiver which is launched on SMS_RECEIVED.
The main point of the app is to vibrate whenever a certain message is received until the user taps a button to make it stop. The activity is only there to allow the user to change settings and press the "Stop" button.
In my onReceive method (BroadcastReceiver), I get the content of the last message received and make the phone vibrate if the message is equal to a certain string. All of that is working perfectly, the problem is when I want to make it stop. Right now, I'm trying to make a "Stop" button appear in the Activity when the phone starts vibrating.
I understand that UI elements should remain in the Activity and so what I'm trying to do is communicate between the Activity and my BroadcastReceiver. I've found here how to do that with an Observer. The problem though is that I want the app to function at any time, even at boot time. It's very easy with a BroadcastReceiver but since it requires the Activity to be shown to allow the user to stop the vibration, I have to start the activity if it isn't started already.
So what I do is this:
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("SMSReceived", true);
context.startActivity(i);
ObservableObject.getInstance().updateValue(true);
The problem is, when there is no instance launched, it creates a new one and sends the extra boolean correctly but the updateValue method doesn't seem to get called at all (due to the previous instance I suppose?) and inversely, when there is an instance launched (in the background) the extra boolean doesn't get passed and the updateValue method gets called correctly.
I suppose I could just launch the Activity on boot and immediately put it in the background but it could cause problems if the user closes the application, at which point it would simply stop working until the user started it again since the Observer would have no instance to send data to.
Do you guys have any idea of what I could do to solve my problem?
If it's not clear I can try to explain further.

Android intent service onClick, or start new activity and then Asynctask

Many times in android apps, users click a button/view and start a new activity, where the new activity pops up in front of what the user was previously looking at, and loads data.
Would there be any difference from a user perspective if the data started loading (from network or disk or both) when the user clicked the button before the next activity started. And then that data was returned to the new activity in a broadcast receiver.
This is compared to starting the process in the oncreate of the activity. Assuming these network and i/o processes only take milliseconds either way, would it make a difference to the user if the methods were started in onCreate of the new activity, or started in the old activity onClick.
First way, starting I/O and changing views after I/O finishes
//first activity
public void onClick(View v){
startActivity(new Intent(this, NewActivity.class);
}
//NewActivity.class
onCreate(Bundle mBundle){
super.onCreate(mBundle);
setContentView(R.layout.mView);
mObject = networkCall(); //after network call, the view objects in this layout will reflect data from the network call
}
second way, starting the I/O in the first activity
//first activity
public void onClick(View v){
IntentService networkCall = new IntentService();
//start network call
startActivity(new Intent(this, NewActivity.class);
}
//second activity on create just sets the view and also broadcast receiver
My GUESS is that in the split second that it takes for the activity to pop up, the data from the intent service could become available. But at the same time, passing data via intent could take just as long making the benefits marginal
Insight appreciated
In my experience the onCreate() of your new activity is called almost instantly from when you call startActivity(). The new activity doesn't show up right way because it has to take time to render your layout.
You could play around with timings yourself by using the Log.d() function. Something like Log.d(TAG, "This happend at: " + System.currentTimeMillis()); at different points in your code to see when things happen. Watch the LogCat while your apps runs and you can decide for your self which way is better.

Exit Android App - Kill all processes

We have a live video streaming app with a lot going on. A user presses the home button. I want the app to be removed from memory. When the app is selected again we have a brand new load. There are a lot of processes going on and we don't want to have to manually manage all the connections, streams, etc. This is how our iPhone version of the app works.
I've read this: Is quitting an application frowned upon?
I don't really care about Androids design patterns here either way. However if someone has an elegant, simple way that all my activities will be removed from the stack when the home button is pressed, and then when the app is reloaded it starts with a fresh main activity, that would be great. Also, I can't seem to ever debug when the home key is pressed in onKeyDown. Its simply not registering. (keyCode == KeyEvent.KEYCODE_HOME) is my check. It picks up back buttons, etc.
Any thoughts?
You can call system.exit(0); but I would still suggest to follow the Android guidelines, and clean everything (as should be) on onPause() or similar method.
Could you just override the onPause method and use the finish() function?
int p = android.os.Process.myPid();
android.os.Process.killProcess(p);
you can do than on button click. Define any static method like exit() and define
android.os.Process.killProcess(android.os.Process.myPid()); in exit method
in the main or first activity.
Then call this method on button click.
If you want to clear all of your activities from the stack, you can broadcast an intent from the activity which initiates the quit like this:
Intent broadcastIntent = new Intent();
broadcastIntent.setAction("CLEAR_STACK");
sendBroadcast(broadcastIntent);
And in every one of your activities that you want to be cleared off the stack you can create a BroadcastReceiver with an inner class:
class ActivitiesBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
finish();
}
}
And register your BroadcastReceiver in the onCreate() method:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("CLEAR_STACK");
BroadcastReceiver r;
r = new ActivitiesBroadcastReceiver();
registerReceiver(r, intentFilter);

Android Yes I want to close the app I opened!

I have an app which I want to open, use, and then at the push of a button close. My question is do I need to keep singleton data of the application status in order to make this happen? I mean if I go with the solution of doing finish on resume() that means that I should need to keep a global data that each activity looks at to close out? This is a fine, albeit awkward way to close an app, but I'll go with it unless I hear another way soon.
Thanks.
PS please do not respond with android will decide when and what to do with your app. I'm sorry but I know my user does want to see this app again after they click Finish! And in the end that's what matters and they are not at all interested in what android needs to do with the app nor should they be.
Maybe you can use System.exit(0) if you want to kill your app.
As came up from comments, you need to do it from the main activity. My way (though there might be a cleaner way) is to add a static handler in the main activity:
static Handler handler = new Handler() {
#Override
public void handleMessage(Message msg)
{
System.exit(2);
}
}
and a static method:
static public Handler getExitHandler()
{
return handler;
}
and in each class I obtain this handler and send it a message when I want to exit:
MyMainActivity.getExitHandler().sendEmptyMessage(0);
again, not so clean, but the same about System.exit(2);
Well, there is a reason why everyone says that, and is the same as "you don't close a web page". I don't know what your users might expect, but this is the way Android/iOS works.
I've used, for something like what you want to do, the flag ACTIVITY_CLEAR_TOP (you set the flag on the intent to open the activity which contains the close button), so in this way the activity stack is deleted, only the current activity remaning: so when you do finish(); the app "closes".
Intent intent = new Intent(this, Some.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
You could pop up a dialogue that informs the user: "the app will close in x seconds" and start a timer that calls finish() on the activity when it ends.

Categories

Resources