Android – Broadcast Receiver

13
63



Android – Broadcast Receiver
Watch More Videos at
Lecture By: Mr. Aditya Dua, Tutorials Point India Private Limited.

Nguồn:https://quydinh.com/

13 COMMENTS

  1. Sir,
    here 9:17 you have made a mistake that <intent-filter> is out of the scope of <receiver>, and still program worked fine. because you have corrected that mistake at off-screen Recording, it would be great if you tell us that where and what you have changed after correcting that mistake.

  2. Main activity:
    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button button=(Button)findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

    Intent i=new Intent();
    i.setAction("com.example.abbaskhan.sendbroadcast");
    i.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
    sendBroadcast(i);
    Log.i("in after click","NA");
    }
    });
    }
    Reciver class:
    class MyReciver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
    Toast.makeText(context,"broadcast recived",Toast.LENGTH_LONG).show();
    }
    }
    Manifist file :

    <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/AppTheme">
    <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.LAUNCHER" />
    </intent-filter>
    </activity>
    <receiver android:name=".MyReciver">
    <intent-filter>
    <action android:name="com.example.abbaskhan.sendbroadcast"></action>
    </intent-filter>
    </receiver>
    </application>
    Still code not working Toast not showing any thing …….help me out here

LEAVE A REPLY

Please enter your comment!
Please enter your name here