2015-09-07 36 views
0

为什么我的广播没有收到这个代码? 我发布我的代码如下。当我运行这个时,我可以看到发送者广播意图。 但是在接收端没有反应。 我已经测试棒棒糖AVD。为什么我的广播没有在这段代码中收到?

  • 接收机清单
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="kr.co.company.mybroadcastreceiver" > 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <receiver android:name=".MyBroadcastReceiver"> 
      <intent-filter> 
       <action android:name="kr.co.company.START_WEB" /> 
      </intent-filter> 
     </receiver> 
    </application> 

</manifest> 
  • 接收机代码

    public class MyBroadcastReceiver extends BroadcastReceiver { 
        @Override 
        public void onReceive(Context context, Intent intent) { 
         Uri uri = Uri.parse("http://www.google.com"); 
         Intent intent1 = new Intent(Intent.ACTION_VIEW, uri); 
         intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
         context.startActivity(intent1); 
        } 
    } 
    
  • 发送方码

    public class MyBroadcastSender extends Activity { 
    
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_my_broadcast_sender); 
    
    Button click = (Button) findViewById(R.id.click); 
    click.setOnClickListener(new Button.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         Intent intent = new Intent(); 
         intent.setAction("kr.co.company.START_WEB"); 
         sendBroadcast(intent); 
        } 
    }); 
    } 
    } 
    

回答

0

如果另一个应用程序正在发送的意图,你应该在清单中添加这个到接收器的定义:

android:exported="true"

+0

谢谢。但它仍然不起作用。 – myoldgrandpa

相关问题