OnReceive无效

时间:2017-03-16 04:02:26

标签: android sms

我的onReceive调用没有调用,所以我的方法不起作用。  这是我的表现:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hobbs.homework_4">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_SMS"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <uses-permission android:name="android.permission.READ_SMS"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:screenOrientation="landscape">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver
            android:name=".MyBroadcastReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action
                    android:name="android.provider.Telephony.SMS_RECEIVED"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>

这是我的广播接收方法:

public class MyBroadcastReceiver extends BroadcastReceiver {
public MyBroadcastReceiver() {
}

@Override
public void onReceive(Context context, Intent intent) {
    // TODO: This method is called when the BroadcastReceiver is receiving
    // an Intent broadcast.

    Bundle bundle = intent.getExtras();

    if(bundle != null){
        Object[] messages = (Object[]) bundle.get("pdus");
        SmsMessage[] sms = new SmsMessage[messages.length];

        for (int n = 0; n < messages.length; n++){
            sms[n] = SmsMessage.createFromPdu((byte []) messages[n]);
        }
        for(SmsMessage s: sms){
            Log.d("MY_BROADCAST-RECEIVER", s.getMessageBody());
        }

        Intent startMyActivityIntent = new Intent(context.getApplicationContext(),MainActivity.class);
        startMyActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(startMyActivityIntent);
    }
    throw new UnsupportedOperationException("Not yet implemented");
  }
}

这是我的主要活动:

public class MainActivity extends AppCompatActivity {

ListView list;
WebView webView;
List<String> sites = Arrays.asList("https://www.google.com", "https://www.yahoo.com", "https://www.apple.com");
String [] url = {"https://www.google.com", "https://www.yahoo.com", "https://www.apple.com"};

MyBroadcastReceiver receiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    list = (ListView)findViewById(R.id.listview);
    final MyListAdapter adapter = new MyListAdapter(this, sites);
    list.setAdapter(adapter);
    list.setDividerHeight(5);

    webView = (WebView)findViewById(R.id.webview);
    webView.setWebViewClient(new WebViewClient(){});

    list.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id){
            Toast.makeText(getApplicationContext(), sites.get(position), Toast.LENGTH_SHORT).show();
            webView.loadUrl("https://www.google.com");
            webView.loadUrl(url[position]);

        }
    });

    receiver = new MyBroadcastReceiver();
    this.sendBroadcast(new Intent("MyBroadcastReceiver"));
    IntentFilter filter = new IntentFilter();
    registerReceiver(receiver,filter);
  }
}

另外,如何将SMS添加到我的默认列表?

这是我的数组适配器:

public class MyListAdapter extends ArrayAdapter<String> {
    private Activity context_;
    private List<String> sites;

    //constructor
    public MyListAdapter(Activity context, List<String> sites){
        super(context,R.layout.row,sites);
        context_ = context;
        this.sites = sites;


    public View getView (int position, View convertView, ViewGroup parent){
        if(convertView == null){
            LayoutInflater inflater = context_.getLayoutInflater();
            convertView = inflater.inflate(R.layout.row,null);
        }

        ViewHolder holder = (ViewHolder) convertView.getTag();

        if(holder == null){
            holder = new ViewHolder(convertView);
            holder.tv_link.setText(sites.get(position));
        }
        else{
            holder.tv_link.setText(sites.get(position));
        }
        return convertView;
    }

    private class ViewHolder{
        TextView tv_link;
        public ViewHolder(View row){
            tv_link = (TextView) row.findViewById(R.id.tv_link);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您的意图过滤器中未定义操作,首先注册接收器然后发送广播。

receiver = new MyBroadcastReceiver();
IntentFilter filter = new IntentFilter("MyBroadcastReceiver");
registerReceiver(receiver,filter);
this.sendBroadcast(new Intent("MyBroadcastReceiver"));