2017-04-09 99 views
1

我有一個應用程序正在運行後臺服務。當檢測到電話時,我想讓該應用程序打開並向我顯示特定的意圖。我應該如何做到這一點。如何以編程方式打開在後臺運行的應用程序

我的代碼是

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中,在檢測到電話呼叫後,我想啓動在後臺運行的應用程序以打開其第一個活動。

回答

0

你可能想尋找到了Android cookbook

你想採取行動的來電,並做了來電號碼的東西。

解決方案:

這可以通過實施廣播接收器和監聽一個TelephonyManager.ACTION_PHONE_STATE_CHANGED行動來實現。

您可能需要做更多的研究;取決於你的目標Android版本!

+0

我已經當一個電話號碼被稱爲動作。我需要的是一個函數,它可以使我在應用程序被銷燬或暫停後,在通話期間啓動我的應用程序 –

0

試試這個link。希望這會幫助你。透明的活動會幫助你一些什麼。

Go through this link also

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

我不希望烤麪包或任何窗口在頂部運行。我希望整個應用程序啓動。 –

相關問題