我是Android开发人员的新手。
我需要创建一个应用程序,当手机收到特定的短信时,该应用程序执行任务(如拍照或打电话)。
到目前为止,我已经设置了一个SMS广播接收器并将消息存储在一个字符串中。
但是当我尝试直接在广播接收器的onReceive功能中使用Camera API或电话呼叫意图时,应用程序无法正常工作并在收到该消息时崩溃。
我不知道该怎么办。
请指导我。
时崩溃记录
if (st.equals("TAKEPHOTO")) {
context.startActivity(new Intent(MediaStore.ACTION_IMAGE_CAPTURE));
}
用于onReceive func
>
com.example.korde.korde E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.korde.korde, PID: 2469
java.lang.RuntimeException: Unable to start receiver com.example.korde.korde.SmsReceiver: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2426)
at android.app.ActivityThread.access$1700(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1272)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.ContextImpl.startActivity(ContextImpl.java:1034)
at android.app.ContextImpl.startActivity(ContextImpl.java:1021)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:311)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:311)
at com.example.korde.korde.SmsReceiver.onReceive(SmsReceiver.java:54)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2419)
at android.app.ActivityThread.access$1700(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1272)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)