我知道有很多这样的问题已经浮出水面,我已经尽力尝试了所有可能遇到的问题,但是我仍然无法使其正常工作。
我的问题是BroadcastReceiver onReceive似乎从未被调用过。 我的代码如下:
<div id="gal">
<img src="http://dummyimage.com/180x120/000/fff?text=FIRST">
<img src="http://dummyimage.com/175x104/f0f/fff">
<img src="http://dummyimage.com/150x100/a3d/fff">
<img src="http://dummyimage.com/278x125/cf5/fff">
<img src="http://dummyimage.com/199x120/e46/fff">
<img src="http://dummyimage.com/207x480/361/fff">
<img src="http://dummyimage.com/400x107/081/fff">
<img src="http://dummyimage.com/50x40/cc3/fff">
<img src="http://dummyimage.com/700x500/233/fff">
<img src="http://dummyimage.com/300x120/a26/fff">
<img src="http://dummyimage.com/301x177/f1d/fff">
<img src="http://dummyimage.com/164x239/d34/fff">
<img src="http://dummyimage.com/200x300/34e/fff">
<img src="http://dummyimage.com/175x120/72a/fff">
<img src="http://dummyimage.com/210x110/112/fff">
<img src="http://dummyimage.com/278x225/644/fff">
<img src="http://dummyimage.com/300x120/dc3/fff">
<img src="http://dummyimage.com/90x104/b30/fff">
<img src="http://dummyimage.com/120x60/bb3/fff">
<img src="http://dummyimage.com/140x125/aa3/fff?text=LAST">
</div>
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
日志消息永远不会显示。
AndroidManifest.xml
class SMSReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Log.d("BroadcastReceiver", "onReceive")
if (intent.action == Telephony.Sms.Intents.SMS_RECEIVED_ACTION) {
Log.d("BroadcastReceiver", "SMS received")
// Will do stuff with message here
}
}
在我的mainActivity中,我尝试了多种方法来实现此目的,但目前只有:
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<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/Theme.AppCompat.Light.NoActionBar">
<activity android:name=".main.MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
<activity android:name=".setup.SetupActivity"
android:screenOrientation="portrait">
</activity>
<receiver
android:name=".SMSReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="1000">
<action
android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
我非常感谢我能获得的所有提示,如果任何代码示例都是用Kotlin编写的,那也很好。 :)
答案 0 :(得分:0)
如果在onReceive()内部进行检查,请勿使用
class SMSReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Log.d("BroadcastReceiver", "onReceive")
Log.d("BroadcastReceiver", "SMS received")
// Will do stuff with message here
}
答案 1 :(得分:0)
我遇到了同样的问题,即使我正在请求所有权限并且一次允许所有权限,但是这只是权限问题,请确保通过应用调试标记确保您具有所有权限。
我使用了以下
private val appPermission = arrayOf(Manifest.permission.READ_SMS, Manifest.permission.RECEIVE_MMS)
在onCreate
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.RECEIVE_SMS)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.RECEIVE_SMS)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.RECEIVE_SMS),
PERMISSIONS_RECEIVE_SMS)
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
} else {
// Permission has already been granted
}
这是onRequestPermission
override fun onRequestPermissionsResult(requestCode: Int,
permissions: Array<String>, grantResults: IntArray) {
when (requestCode) {
PERMISSIONS_RECEIVE_SMS -> {
// If request is cancelled, the result arrays are empty.
if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
Log.d(TAG, "PERMISSIONS_RECEIVE_SMS permission granted")
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_SMS)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_SMS)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.READ_SMS),
PERMISSIONS_REQUEST_READ_SMS)
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
} else {
// Permission has already been granted
}
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Log.d(TAG, "PERMISSIONS_RECEIVE_SMS permission denied")
}
return
}
PERMISSIONS_REQUEST_READ_SMS -> {
// If request is cancelled, the result arrays are empty.
if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
Log.d(TAG, "PERMISSIONS_REQUEST_READ_SMS permission granted")
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Log.d(TAG, "PERMISSIONS_REQUEST_READ_SMS permission denied")
}
return
}
// Add other 'when' lines to check for other
// permissions this app might request.
else -> {
// Ignore all other requests.
}
}
}