我已经创建了一个合适的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