如何以编程方式打开在后台运行的应用程序

时间:2017-04-09 18:57:14

标签: java android

我有一个运行后台服务的应用程序。当检测到电话时,我希望该应用程序打开并向我显示特定的意图。我该怎么办呢。

我的代码是

MainActivity.java

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import java.lang.reflect.Method;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void startService(View view){
        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        PhoneStateListener phoneStateListener = new PhoneStateListener(){
            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                String number = incomingNumber;
                Log.d("gaandu", number);
                if(state == TelephonyManager.CALL_STATE_RINGING){
                    Toast.makeText(MainActivity.this, "incoming call from" + incomingNumber, Toast.LENGTH_SHORT).show();
                }
                if(state == TelephonyManager.CALL_STATE_OFFHOOK){
                    Toast.makeText(MainActivity.this, "Phone is currently in a call", Toast.LENGTH_SHORT).show();
                }
                if(state == TelephonyManager.CALL_STATE_IDLE){
                    Toast.makeText(MainActivity.this, "Phone is neither Ringing nor in a Call", Toast.LENGTH_SHORT).show();
                }
            }
        };
        telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
    }
    public void stopService(View view){
        Intent i = new Intent(MainActivity.this, MyService.class);
        stopService(i);
    }
}

MyService.java

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;


public class MyService extends Service {
    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
        return START_STICKY;

    }
    @Override
    public void onDestroy() {

        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_SHORT).show();

    }
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

的AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.admin.abab">
    <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <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">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name=".MyService"
            android:exported="false"
            />

    </application>

</manifest>

在MainActivity.java中,在检测到电话后,我想启动我在后台运行的应用程序以打开其第一个活动。

2 个答案:

答案 0 :(得分:0)

您可能需要查看Android cookbook

  

您希望对来电进行操作,并使用来电号码执行操作。

     

解决方案:

     

这可以通过实现广播接收器并侦听TelephonyManager.ACTION_PHONE_STATE_CHANGED操作来实现。

你可能需要做更多的研究;取决于您要定位的Android版本!

答案 1 :(得分:0)

试试这个link。希望这会帮助你。透明的你的活动会帮助你做些什么。

Go through this link also

getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);