How should I implement a broadcast receiver & filter so that it can response to multiple intents.
private BroadcastReceiver myReceiver;
IntentFilter myFilter = new IntentFilter();
onCreate():
myFilter.addAction("first");
myFilter.addAction("second");
myReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// do different actions according to the intent
}
};
registerReceiver(myReceiver, myFilter);
from my fragment:
Intent i = new Intent("first"); sendBroadcast(i);
Intent i = new Intent("second"); sendBroadcast(i);
Thanks
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action != null) {
if(action.equals("action1") {
// CODE
} else if (action.equals("action2") {
// CODE
}
}
}
Related
I am trying to show a countdown in a textview from a loop inside an Intent Service. I am using the result receiver class for the communication between Intent Service and Activity. It works fine when I start the service for the first time. The textview shows the countdown for each time the loop runs in the service.
But when I close and launch the app again the textview doesn't show the countdown and only shows the hard coded text, while on the other hand the service stills runs in the background.
Here is my code snippet for the MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
public static final String RECEIVER_INTENT_EXTRA_NAME = "message_receiver_intent_extra";
private static final String TAG = "MainActivity";
private Intent intent;
MyIntentService myIntentService;
public TextView serviceCountdown;
private Button startButton, stopButton;
private Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
myIntentService = new MyIntentService();
startButton = findViewById(R.id.startServiceButton);
stopButton = findViewById(R.id.stopServiceButton);
startButton.setOnClickListener(this);
stopButton.setOnClickListener(this);
handler = new Handler();
serviceCountdown = findViewById(R.id.serviceCountdown);
MessageReceiver messageReceiver = new MessageReceiver(handler);
// send intent service
intent = new Intent(this, MyIntentService.class);
intent.putExtra(RECEIVER_INTENT_EXTRA_NAME, messageReceiver);
}
#Override
public void onClick(View v) {
if (startButton.equals(v)) {
ContextCompat.startForegroundService(getApplicationContext(), intent);
}
if (stopButton.equals(v)){
stopService(intent);
}
}
public class MessageReceiver extends ResultReceiver {
public MessageReceiver(Handler handler) {
super(handler);
}
#Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
super.onReceiveResult(resultCode, resultData);
if (resultCode == 1 && resultData != null){
final String countdown = resultData.getString("countdown");
handler.post(new Runnable() {
#Override
public void run() {
serviceCountdown.setText(countdown);
}
});
}
}
}
}
And here is my code for the Intent Service Class
public class MyIntentService extends IntentService {
private static final String CHANNEL_ID = "my_channel_id";
private static final String TAG = "MyIntentService";
public MyIntentService() {
super("MyIntentService");
setIntentRedelivery(true);
}
#Override
public void onCreate() {
super.onCreate();
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent,0);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("My Service Title")
.setContentText("This is sample notification text")
.setSmallIcon(R.drawable.ic_battery)
.setContentIntent(pendingIntent)
.build();
startForeground(1, notification);
}
#Override
protected void onHandleIntent(#Nullable Intent intent) {
ResultReceiver resultReceiver = intent.getParcelableExtra(MainActivity.RECEIVER_INTENT_EXTRA_NAME);
Log.d(TAG, "onHandleIntent: called");
synchronized (this) {
for (int i = 10; i >= 0; i--) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d(TAG, "Service is looping : " + i);
Bundle bundle = new Bundle();
bundle.putString("countdown", String.valueOf(i));
resultReceiver.send(1, bundle);
}
}
}
#Override
public void onDestroy() {
Log.d(TAG, "onDestroy: called");
super.onDestroy();
}
}
In the real project, my intention is not to using the loop to show a countdown. It is just for testing and debugging purpose.
Use Local Broadcast Receiver from your service to activity. Now you getting the ResultReceiver from the MainActivity Intent. When the activity destroyed intents are also destroyed.
Use this Broadcast Receiver code in your service class.
LocalBroadcastManager localBroadcastManager =
LocalBroadcastManager.getInstance(this);
Intent sendIntent = new Intent(INTENT_ACTION_KEY);
sendIntent.putExtra(DATA_KEY, data);
localBroadcastManager.sendBroadcast(sendIntent);
Get this local broadcast receiver in your activity.
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
//Perform your logic.
}
}
};
Make sure you register this broadcast when activity starts and unregister it when stop.
#Override
public void onStart() {
super.onStart();
LocalBroadcastManager.getInstance(this).registerReceiver((broadcastReceiver),
new IntentFilter(INTENT_ACTION_KEY));
}
#Override
public void onStop() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(broadcastReceiver);
super.onStop();
}
I'm trying to set up a alarm to start a job on a certain time but my alarm is never trigger and I don't understand what i'm missing. I'm using android api 22.
Thank you.
public class MainActivity extends ProtectedActivity {
#Override
public void onResume() {
super.onResume();
if (terminalSettings.GetSettlementEnabled())
{
startAlarmManager();
}
}
private void startAlarmManager()
{
Intent i = new Intent(MainActivity.this,
AlarmBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(MainActivity.this, 0,
i, 0);
AlarmManager am =( AlarmManager)getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, pi); //
Millisec * Second * Minute
}
}
class AlarmBroadcastReceiver
public class AlarmBroadcastReceiver extends BroadcastReceiver {
public AlarmBroadcastReceiver() {
super();
}
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Alarm !!!!!!!!!!",
Toast.LENGTH_LONG).show(); // For example
}
}
In the manifest I added those line
<receiver android:name=".AlarmBroadcastReceiver"></receiver>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
You need to call registerReceiver
public static final String Broadcast_PLAY_NEW_AUDIO = "Some key so you know which broadcast is which";
//Create Receiver
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//Do stuff
}
};
//Register receiver
IntentFilter filter = new IntentFilter(HomeActivity.Broadcast_PLAY_NEW_AUDIO);
registerReceiver(receiver, filter);
//Send Broadcast
Intent broadcastIntent = new Intent(Broadcast_PLAY_NEW_AUDIO);
sendBroadcast(broadcastIntent);
In my Application I have a Broadcast receiver (registered in manifest) class from which I want to send an Intent to MainActivity. Therefore I have another broadcast receiver (dynamically registered) in MainActivity and an Intent Filter. But I don't receive the Intent in Main Activity.
This is the code:
public class SmsReceiver extends BroadcastReceiver {
public static final String SMS_BUNDLE = "pdus";
public SmsReceiver(){
}
String TAG = SmsReceiver.class.getSimpleName();
#Override
public void onReceive(Context context, Intent intent){
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] sms = (Object[]) bundle.get(SMS_BUNDLE);
String str = "";
for (int i=0; i < sms.length; i++) {
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);
String smsbody = smsMessage.getMessageBody().toString();
str += smsbody;
}
Intent bcIntent = new Intent();
bcIntent.setAction("SMS_RECEIVED_ACTION");
bcIntent.addCategory(Intent.CATEGORY_DEFAULT);
bcIntent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
bcIntent.putExtra("message", str);
context.sendBroadcast(bcIntent);
}
}
}
and in the MainActivity:
public class MainActivity extends AppCompatActivity
{
public boolean receivedSMS;
public String displaySMS;
private BroadcastReceiver iReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String qqq = intent.getExtras().getString("message");
info2(intent.getExtras().getString("message"));
evalMsg(qqq);
}
};
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
graphInit();
timerInit();
smnInit();
}
#Override
protected void onResume(){
super.onResume();
IntentFilter iFilter = new IntentFilter();
iFilter.addAction("SMS_RECEIVED_ACTION");
registerReceiver(iReceiver, iFilter);
}
#Override
protected void onPause(){
unregisterReceiver(iReceiver);
super.onPause();
}
Does anyone know why the broadcast receiver in the main activity doesnt receive the intent? There should be a text displayed in the TextView but it is never shown. thanks for any idea
When receiving the intent via SmsReceiver you can't know if the Activity is up and in what state.
What you should consider is using startActivity(android.content.Intent) from within your SmsReceiver.onReceive(). This way you'll have the same intent object available within Activity.onCreate (just call the getIntent() method)
But if you want to stick to your original design of double broadcasts, then here is your answer.
I't trying to implement a local VpnService to have my app do some tasks, but I'm a little confused as to how to stop it one it started. The VpnService class and client activity are based on this repo:
https://github.com/hexene/LocalVPN
The caller activity is basically this:
public class MainActivity extends AppCompatActivity {
private static final int VPN_REQUEST_CODE = 0x0F;
private boolean waitingForVPNStart;
private BroadcastReceiver vpnStateReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (LocalVPNService.BROADCAST_VPN_STATE.equals(intent.getAction()))
if (intent.getBooleanExtra("running", false))
waitingForVPNStart = false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button vpnButton = (Button)findViewById(R.id.vpn);
vpnButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startVPN();
}
});
final Button vpnStopButton = (Button)findViewById(R.id.stopVpnButton);
vpnStopButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stopVPN();
}
});
waitingForVPNStart = false;
LocalBroadcastManager.getInstance(this).registerReceiver(vpnStateReceiver,
new IntentFilter(LocalVPNService.BROADCAST_VPN_STATE));
}
private void startVPN() {
Intent vpnIntent = VpnService.prepare(this);
if (vpnIntent != null)
startActivityForResult(vpnIntent, VPN_REQUEST_CODE); //Prepare to establish a VPN connection. This method returns null if the VPN application is already prepared or if the user has previously consented to the VPN application. Otherwise, it returns an Intent to a system activity.
else
onActivityResult(VPN_REQUEST_CODE, RESULT_OK, null);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == VPN_REQUEST_CODE && resultCode == RESULT_OK) {
waitingForVPNStart = true;
startService(new Intent(this, LocalVPNService.class));
enableButton(false);
}
}
What confuses me is: how would I call the service's onDestroy() method or something similar if I don't keep an instance if it in my main activity?
I looked at this answer and this and seen implementations of stopService, but I'm not sure how to handle the Intent, because it's not only used to call startService() but also involved in calling VpnService.prepare().
Edit: I tried
stopService(new Intent(this, LocalVPNService.class)); but it doesn't stop it. I tried stopService(vpnIntent); and IT WORKS, but makes my app crash/close.
In your LocalVPNService class create a new broadcast:
private BroadcastReceiver stopBr = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if ("stop_kill".equals(intent.getAction())) {
stopself();
}
}
};
and in the onCreate method add this:
LocalBroadcastManager lbm =
LocalBroadcastManager.getInstance(this);
lbm.registerReceiver(stopBr, new IntentFilter("stop_kill"));
in your activity:
Intent intent = new Intent("stop_kill");
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
I am developing android app which work as battery indicator, open activity when battery low or full.
It works fine when i call finish() in Main activity from onCreate event after start of service.
but when i comment finish method it open activity multiple times, which i open from BroadCast receiver.
Here is MainActivity Code:
public class Main extends Activity {
private MyService service;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (service == null) {
Intent i = new Intent(this, MyService.class);
startService(i);
}
finish();
}}
Here is my service code:
I think i am doing something wrong when i start Activity.
At line
getApplication().startActivity(intent);
Complete Service Code is:
public class MyService extends Service{
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("MyService", "onStartCommand");
// do not receive all available system information (it is a filter!)
final IntentFilter battChangeFilter = new IntentFilter(
Intent.ACTION_BATTERY_CHANGED);
// register our receiver
this.registerReceiver(this.batteryChangeReceiver, battChangeFilter);
return super.onStartCommand(intent, flags, startId);
}
private final BroadcastReceiver batteryChangeReceiver = new BroadcastReceiver() {
#Override
public void onReceive(final Context context, final Intent intent) {
checkBatteryLevel(intent);
}
};
private void checkBatteryLevel(Intent batteryChangeIntent) {
// some calculations
final int currLevel = batteryChangeIntent.getIntExtra(
BatteryManager.EXTRA_LEVEL, -1);
final int maxLevel = batteryChangeIntent.getIntExtra(
BatteryManager.EXTRA_SCALE, -1);
final int percentage = (int) Math.round((currLevel * 100.0) / maxLevel);
if(percentage==100)
{
Intent intent = new Intent(getBaseContext(), Last.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(intent);
}
if(percentage==15)
{
Intent intent = new Intent(getBaseContext(), Last.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(intent);
}
}}
And this is my last "Last.cs" activity which is opening multiple time.
but it works fine when i call finish() into Main Activity.
public class Last extends Activity {
Button btnCancel;
Uri notification;
Ringtone r;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_last);
notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
r = RingtoneManager.getRingtone(getApplicationContext(), notification);
r.play();
btnCancel = (Button) findViewById(R.id.stopsound);
btnCancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
r.stop();
}
});
} }
Make your Last activity launchMode as singleTask in Manifest
<activity
android:name=".Last"
android:configChanges="orientation|screenSize"
android:launchMode="singleTask"
>
</activity>