我知道有很多这样的问题已经浮出水面,我已经尝试了所有我能遇到的问题,但我仍然无法让它发挥作用。
我的问题是 BroadcastReceiver onReceive 似乎永远不会被调用。我的代码如下:
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
}
}
Run Code Online (Sandbox Code Playgroud)
日志消息永远不会出现。
AndroidManifest.xml
<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> …Run Code Online (Sandbox Code Playgroud) 我有当前的设置:
class Interface1
{
public:
virtual ~Interface1() {}
virtual void DoSomething() = 0;
};
class Interface2 : public virtual Interface1
{
public:
virtual ~Interface2() {}
virtual void DoSomething() override = 0;
virtual void DoSomethingElse() = 0;
};
class MyClass1 : public Interface1
{
public:
MyClass1();
void DoSomething() override;
};
class MyClass2 : public Interface2
{
public:
MyClass2();
void DoSomething() override;
void DoSomethingElse() override;
};
int main()
{
std::map<std::string, boost::any> items;
items.insert(make_pair("item1", shared_ptr<Interface1>(new MyClass1())));
items.insert(make_pair("item2", shared_ptr<Interface2>(new MyClass2())));
auto object = items.at("item2"); …Run Code Online (Sandbox Code Playgroud) 如何将派生自接口的类传递给以接口为参数的函数?
我有一个接口和一个类设置类似这样的东西。
class Interface
{
public:
virtual ~Interface() {}
virtual void DoStuff() = 0;
};
class MyClass : public Interface
{
public:
MyClass();
~MyClass();
void DoStuff() override;
};
void TakeAnInterface(std::shared_ptr<Interface> interface);
int main()
{
auto myInterface = std::make_shared<MyClass>();
TakeAnInterface(myInterface);
}
Run Code Online (Sandbox Code Playgroud)
编译器抱怨No matching function call to TakeAnInterface(std::shared_ptr<MyClass>&)。为什么功能TakeAnInterface不能接收Interface类而不是MyClass?