to bring app to front on receive of a call

2019-05-26 01:57发布

I want to bring my app to front of phone call answer screen when i receive a call. I did every coding part to happen after receiving a call. But the app is not coming to front. Its just open and stays below the phone call answer screen.
I want to bring my app to front of this screen.

I did something like below:

Intent i = new Intent(); i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
ComponentName cn = new ComponentName(this, MainActivity.class);
i.setComponent(cn); 
startActivity(i);

But there is no change.

2条回答
聊天终结者
2楼-- · 2019-05-26 02:15

You need to Implement one service to invoke application from background.

public class LaunchApplication extends Service {

    String PubNubmessage;

    public LaunchApplication() {
        super();

    }

    @Override
    public void onCreate() {

        super.onCreate();


    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        LaunchApplicationWhenReceivedCall();

        return START_NOT_STICKY;
    }

    private void LaunchApplicationWhenReceivedCall() {
        Intent intent1 = new Intent(getApplicationContext(), HomeActivity.class);

        intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent1);
    }

    @Override
    public IBinder onBind(Intent intent) {

        return null;
    }

}

Now start service when your app detects call is received.

查看更多
forever°为你锁心
3楼-- · 2019-05-26 02:21

Add the priority to the receiver..this will help to be called prior than the native incoming call by the android os

<receiver android:name="com.companyname.receivers.InComingCallReceiver" >
        <intent-filter android:priority="2147483647" >
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
 </receiver>

Hope this would be helpfull

查看更多
登录 后发表回答