我创建了一个应用程序可以接收短信的项目。它进展顺利,但当我试图将它实施到我的主项目时#34;它不会起作用。我试图将广播接收器实现到我的片段。我试过谷歌和不同的论坛,大多数人说这是权限,但我已经设置:(。真的希望我可以得到一些清晰度。
我的错误日志说明了这一点。
FATAL EXCEPTION: main
Process: c.timno.smsgsm20, PID: 23636
java.lang.RuntimeException: Error receiving broadcast Intent { act=android.provider.Telephony.SMS_RECEIVED flg=0x8000010 (has extras) } in c.timno.smsgsm20.ThirdFragment$1@423ff210
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:788)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5103)
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:788)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at c.timno.smsgsm20.ThirdFragment.processReceive(ThirdFragment.java:75)
at c.timno.smsgsm20.ThirdFragment$1.onReceive(ThirdFragment.java:55)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:778)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5103)
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:788)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604)
at dalvik.system.NativeStart.main(Native Method)

我的机器人清单。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="c.timno.smsgsm20">
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_SMS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_heicon"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".Splash"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity"/>
</activity>
<!--
ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information.
-->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings"></activity>
</application>
</manifest>
&#13;
我的片段
package c.timno.smsgsm20;
import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by TimNo on 2016-09-09.
*/
public class ThirdFragment extends Fragment{
private View view ;
View myView;
BroadcastReceiver receiver=null;
@SuppressWarnings("ResourceType")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.third_layout, container, false);
return inflater.inflate(R.layout.third_layout, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
view = getActivity().findViewById(R.id.content_frame);
IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context arr0, Intent arr1) {
processReceive (arr0, arr1);
}
};
getActivity().registerReceiver(receiver,filter);
}
public void onDestroy() {
super.onDestroy();
getActivity().unregisterReceiver(receiver);
}
public void processReceive (Context context, Intent intent){
Toast.makeText(context, "You got a message",Toast.LENGTH_LONG).show();
TextView lbs = (TextView)view.findViewById(R.id.textvview);
Bundle bundle = intent.getExtras();
Object[] objArr= (Object[])bundle.get("pdus");
String sms ="";
for (int i=0;i<objArr.length;i++){
SmsMessage smsMsg = SmsMessage.createFromPdu((byte[])objArr[i]);
String smsBody = smsMsg.getMessageBody();
String senderNumber = smsMsg.getDisplayOriginatingAddress();
sms +="From: " + senderNumber+"\nContent: "+smsBody+"\n";
}
lbs.setText(sms);
}
}
&#13;
提前谢谢大家!
答案 0 :(得分:0)
解决方案是代码应该在我的MainActivity.java中。我认为broadcastreceiver只能从MainActivity调用而不是在fragment.java中调用。我把代码放在我的onResume上并且它有效:D。我的textview(我加载的短信)位于片段xml文件中。
BroadcastReceiver receiver=null;
@Override
protected void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context arr0, Intent arr1) {
processReceive (arr0, arr1);
}
};
registerReceiver(receiver,filter);
}
public void onDestroy() {
super.onDestroy();
unregisterReceiver(receiver);
}
public void processReceive (Context context, Intent intent){
Toast.makeText(context, "You got a message",Toast.LENGTH_LONG).show();
TextView lbs = (TextView)findViewById(R.id.textvviewtsmsruta);
Bundle bundle = intent.getExtras();
Object[] objArr= (Object[])bundle.get("pdus");
String sms ="";
for (int i=0;i<objArr.length;i++){
SmsMessage smsMsg = SmsMessage.createFromPdu((byte[])objArr[i]);
String smsBody = smsMsg.getMessageBody();
String senderNumber = smsMsg.getDisplayOriginatingAddress();
sms +="From: " + senderNumber+"\nContent: "+smsBody+"\n";
}
lbs.setText(sms);
}
&#13;