我已經創建了一個合適的BoradcastReceiver
,在Manifest.xml
中註冊了它,這裏是我的問題:如果我的應用程序已經啓動並掛在後臺,撥打號碼會將其置於前面。如果尚未啓動,則撥打號碼將不起作用。
我該如何解決這個問題?如果這一點很重要,我會用MIUI6對小米Mi4進行測試。通過撥打號碼發起活動
下面的代碼(我用的Scala):
的manifest.xml:
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
...
<receiver android:name="DialerGate" android:enabled="true" android:exported="true">
<intent-filter android:priority="1">
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
</intent-filter>
</receiver>
廣播接收器:
class DialerGate extends BroadcastReceiver {
def onReceive(context: Context, intent: Intent) =
if (intent.getAction equals Intent.ACTION_NEW_OUTGOING_CALL) {
val phoneno = intent.getExtras getString Intent.EXTRA_PHONE_NUMBER
val prefs = context.getSharedPreferences("prefs", Context.MODE_PRIVATE)
val number = prefs.getString(AbstractKit.LAUNCH_NUMBER, null)
Log.d("WALLET-PHONE", s"Dialed number: $phoneno, saved number: $number")
Log.d("WALLET-PHONE-OK", (number == phoneno).toString)
val i = new Intent
i.setClassName("com.app.wallet", "com.app.wallet.MainActivity")
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP)
val appContext = context.getApplicationContext
appContext.startActivity(i)
//if (number == phoneno) context startActivity new Intent(context, target)
//context stopService intent
}
}
難道你不能讓你的應用程序在啓動時運行嗎? http://stackoverflow.com/questions/10428510/how-to-start-launch-application-at-boot-time-android/10428665#10428665 – JesterXiii
@JesterXiii,不,這不是我想要的。我的目標是啓動撥打特定號碼的應用程序。 – Anton
@Anton當他們在Manifest中註冊時,廣播的工作方式是,如果它沒有運行,它們將被調用來喚醒進程。 – JoxTraex