How to launch an app by dialing a specific number? - android

i am developing an android application, in which i want application icon to be hidden when toggle button is clicked. I am done with this task, Now i want when a specific code is dialed from launcher then launcher activity should be fired again.
i have implemented this too, but dialer code is unable to let me into the application again.

Try this
//Java Code
String phoneNumber = intent.getStringExtra("android.intent.extra.PHONE_NUMBER");
if(phoneNumber.equals("#588637#")) {
//do your stuff here
Intent intent = new Intent();
intent.setAction("com.mycompany.DO_SOMETHING");
context.startActivity(intent);
}
//Manifest
<activity
android:name=".OtherAppActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.mycompany.DO_SOMETHING" />
</intent-filter>
</activity>

Related

How to use the dialer while still viewing application information?

Situation:
Users of my application need to call their contacts and still being able to view special contact information.
Initiating a call is easy with the following code:
private void performDial(String numberString) {
if ( checkSelfPermission(android.Manifest.permission.CALL_PHONE) ==
PackageManager.PERMISSION_GRANTED) {
if (!numberString.equals("")) {
Uri number = Uri.parse("tel:" + numberString);
Intent dial = new Intent(Intent.ACTION_CALL, number);
startActivity(dial);
}
}
}
However the GUI of the dialer will hide the contact information. In order to view this contact information a user has to perform two steps:
Push the home button This will shrink the dialer to a small “icon”.
Use the left button to select the correct application in the list of
running apps. Showing the relevant information while still having
the dialer active in the form of the “icon”.
Questions:
Is it possible to start the dialer intent in the “icon” form?
Is it possible to perform the two steps programmatically?
What I tried already:
The Intent parameters Flags and Extras, don’t seem to give options to start the dialer in the “icon” form.
The following code fragment emulates a home button:
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
And the following fragment could bring the main application back in front.
Intent intent = new Intent(this, MainActivity.class);
intent. addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
However both fragments won’t work after the dialer is active. And the dialer will not start if these fragments are executed directly after starting the intent.
Here is the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="copec.test2app">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<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"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ShowWebsite"> </activity>
<receiver android:name="copec.test2app.OutCallLogger" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
</application>
Any help is appreciated.
You can't hide the dialer. What you can do is to move your app to the foreground on top of the dialer. After launching the dialer with startActivity() you should wait until the call is intiated. You can do that by monitoring the outgoing call state, or just by simply waiting a few seconds (which you can do by wrapping the following code in a Runnable and then posting it to Handler using postDelayed(). Then, to move your app to the foreground, do this:
PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage("your.package.name");
getApplicationContext().startActivity(intent);

Home Button in Home Screen App android

I am working on app which will serve as Home Screen Lock. The app will support only 4.1 and above versions. The issue is handling the Home Button. I understand that we cannot override/disable Home Button, so it has to be handled a different way. I have been struggling for almost 2 weeks and tried many umpteenth ways but none of them working as expected. Main Activity is the one which will be on top and user need to enter his set password here to unlock the device. I have set my MainActivity as Home so the user is prompted to select between default launcher and my app. So expected action is to select my app as Launcher 'always'. Also I have set an flag in broadcast receiver whenever MainAcitivity is started. This flag is used in MainActivity to launch system Launcher in case MainActivity isn't started by broadcast receiver.
Everything is working fine till this point and approach. Issue is when I click on my app to change password or access other features, it doesn't open the app and opens the system launcher. The reason is it checks the flag in Main Activity and since it is not started by broadcast receiver, it doesn't launch MainActivity now and starts the system launcher. Below are code snippets:
Manifest File:
<activity
android:name=".MainActivity"
android:excludeFromRecents="true"
android:label="#string/app_name"
android:launchMode="singleTop"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</activity>
My Broadcast Receiver
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF) || action.equals(Intent.ACTION_USER_PRESENT)) {
AppSession.getInstance().setReceiverCall(true);
MainActivity.isStarted = true;
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
context.startActivity(i);
}
Main Activity where I check the flag and start the system launcher. I amy doing this on onResume
if (!MainActivity.isStarted) {
Intent intent = new Intent();
intent.setComponent(new ComponentName(mPackageName, mName));
startActivity(intent);
}
mPackageName & mName are system launcher name.
I tried other approaches as well but only one of the thing is resolved. Either home button is handled correctly and then my app isn't launched. If app is launched then on Home button press from anywhere it always brings my MainActivity on top.
I know this is doable as its done in many ScreenLock apps but somehow I am missing something. Please advise.

Android - Home Screen Life Cycle

I am developing an Android Home Screen, I seem to be experiencing problems. Whenever I start an application from that home screen, its lifecycle ends up to being destroyed(I Knew this because I used LogCat and it does print my code in the OnDestroyed method). I only want it to only be paused not completely destroyed because I am running a little long processes in the oncreate. I only wanted the onCreate to be called once, which is when the device boots. In my case, since whenever my home screen starts an application, it is destroyed. And whenever I press the HOME button, it goes through from on create -> on start -> on resume.
The following is my codes, can you state if I am doing something wrong. Thanks.
My Code in Starting an Application:
public void startAppByAppName(String appName) {
String mainActivity = "";
String packageString = "";
Intent intent = getPackageManager().getLaunchIntentForPackage(appName);
mainActivity = intent.getComponent().getClassName();
packageString = intent.getComponent().getPackageName();
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(new ComponentName(packageString, mainActivity));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
My Manifest:
<application
android:allowBackup="true"
android:icon="#drawable/home_icon"
android:label="#string/app_name"
android:theme="#style/AppTheme" android:persistent="true">
<activity
android:name="com.steven.welcomescreen.WelcomeScreenActivity"
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
android:stateNotNeeded="true"
android:screenOrientation="landscape"
android:theme="#android:style/Theme.Black.NoTitleBar">
<intent-filter>
<category android:name="android.intent.category.HOME"/>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>
If there is some parts of my code which you want to clarify, just comment below so I could supply you with information. I really need help. Thanks you very much.

Get instance of current android.intent.category.LAUNCHER activity

I have created a library project that I am sharing across several apps. I implemented a simple session expiration feature that will kick the user back to the login screen after a certain time period.
The login screen activity is my main activity, so in the manifest it looks like this:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.Sherlock.Light.DarkActionBar"
android:name="com.blah.application.MyApplication" >
<activity
android:name="com.blah.activity.LoginScreenActivity"
android:label="#string/title_activity_main"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
When the session is expired, I want to kick the user back to the login screen, but I don't want to hardcode the name of the activity because it might be different depending on the specific app that is using the library. Here's what I was doing before:
Intent intent = new Intent(context, LoginScreenActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
This doesn't work if the app's main activity is something different than LoginScreenActivity. I don't want to hardcode "LoginScreenActivity.class", I want to programmatically determine the name of the main class and then direct the user to that activity...can someone help me out?
Thanks in advance!
EDIT
I found a way to accomplish the same end result, but it's definitely not great. Since there is a certain amount of configuration necessary for me to deploy a new app using the same library (strings, bools, etc), I added a string to the strings.xml file for the specific app that defines the "main" activity name for that app:
<string name="mainClassName">com.blah.specificapp.activity.SpecificAppLoginScreenActivity</string>
Then I can get a handle on that class by name and redirect the user there:
Class<?> clazz = null;
try
{
clazz = Class.forName(context.getString(R.string.mainClassName));
}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
if(clazz != null)
{
Intent intent = new Intent(context, clazz);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
}
I know this is a god-awful solution, but it works. Like I said, there is a certain amount of configuration I have to do for each new app anyway, so adding one more string isn't a huge deal it's just not very elegant. I'd appreciate any suggestions that can accomplish the same goal without using my hack.
You can request a launch Intent from the PackageManager, using:
Intent launchIntent = PackageManager.getLaunchIntentForPackage(context.getPackageName());
This will return an Intent that you can use to launch the "main" activity (which I assume is your "login" activity). Just add Intent.FLAG_ACTIVITY_CLEAR_TOP to this and you should be good to go.
How about using a mime-type on your intent filter.
<activity android:name=".LoginActivity"
android:exported="true" android:launchMode="singleTop" android:label="#string/MT">
<intent-filter>
<category android:name="android.intent.category.DEFAULT"/>
<action android:name="com.foo.ACTION_LOGIN" />
<data android:mimeType="application/x.foo.com.mobile.login" />
</intent-filter>
</activity>
And launch the activity as follows:
Intent intent = new Intent();
intent.setAction("com.foo.ACTION_LOGIN");
intent.setType("application/x.foo.com.mobile.login");
startActivity(myIntent);
Thus the intent will be serviced by whatever activity is registered with this action/mime-type pair. I am not certain, but I think if that activity is hosted in the same app, it may be chosen first.

Forwarding intents in Android

I am trying to capture audio/* mimetype action.View intents and forward them to another application (chosen by the user). The problem is that, while I am able, from a file manager, to select my Activity on file opening (through the app chosen dialog), I am not able to forward the intent to another activity (not the same one).
This is the manifest part about the activity:
<activity
android:label="#string/app_name"
android:name=".TestMimeActivity" >
<intent-filter >
<action android:name="android.intent.action.VIEW" />
<data android:mimeType="audio/*" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
This is the onStart code in the activity:
#Override
protected void onStart() {
super.onStart();
setContentView(R.layout.main);
Intent intent = getIntent();
startActivity(intent);
}
I obtain only an infinite loop. I would like to open on the activity che app chosen dialog (preferably without my app listed but I can also tolerate the whole list). Is it possible? How can I achieve this?
Thanks
Tobia Loschiavo
From your comments above I think you are looking for createChooser. You should modify your code to look like this:
Intent intent = getIntent();
startActivity(Intent.createChooser(intent, "Select application"));

Categories

Resources