Android, Is it possible to run the app from dialer?

后端 未结 3 997
一个人的身影
一个人的身影 2021-02-06 13:33

I need (if its possible) to add an ability to my project to be run when user enters a code such as #1234# in dialer. I\'m not sure it is possible. Currently when i lunc

相关标签:
3条回答
  • 2021-02-06 14:10

    The accepted answer by Priyank is right. I just wanted to point out some corrections in the code in the Intent class

    @Override
    public void onReceive(Context context, final Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){
            String phoneNumber = intent.getExtras().getString(Intent.EXTRA_PHONE_NUMBER);
    
            if(phoneNumber.equals("#1234#")){
                Intent intent1 = new Intent(context, YourActivity.class);
                intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent1);
            }
    
        }
    }
    
    0 讨论(0)
  • 2021-02-06 14:12

    You can start your application activity class by dialer but for that your app should running in background. for that you should implement a class which extends to BroadcastReceiver. follow this reference code.

    public class Example extends BroadcastReceiver 
    {
    
        @Override
        public void onReceive(Context context, final Intent intent) {
    
          if (intent.getAction().equals(android.intent.action.NEW_OUTGOING_CALL)) {
           String phoneNumber = intent.getExtras().getString( android.intent.extra.PHONE_NUMBER );
    
             if(phoneNumber.equals("#1234#")) { 
                Intent intent1 = new Intent(context , YourActivity.class);
                intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
                context.startActivity(intent1);
           }
    
          }
    
        }
    
    } 
    
    0 讨论(0)
  • 2021-02-06 14:24
    @Override
    public void onReceive(Context context, final Intent intent) {
    
        if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
            String phoneNumber = intent.getExtras().getString(Intent.EXTRA_PHONE_NUMBER);
    
            if (phoneNumber.equals("#1234#")) {
                Intent intent1 = new Intent(context, YourActivity.class);
                intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent1);
                setResultData(null);
            }
    
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题