【发布时间】:2015-11-18 21:56:10
【问题描述】:
我创建了一个合适的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
}
}
【问题讨论】:
-
你不能让你的应用在启动时运行吗? stackoverflow.com/questions/10428510/…
-
@JesterXiii,不,这根本不是我想要的。我的目标是启动一个拨打特定号码的应用程序。
-
@Anton 广播在 Manifest 中注册时的工作方式是,如果进程未运行,它们将被调用以唤醒进程。
-
这可能会有所帮助 - stackoverflow.com/questions/17122085/…
-
你的意思是,如果应用程序在后台它可以工作,如果不是它就不行?