How to finish activity in broadcastreceiver onCallEnded() - android

I have a question, how to finish activity in brodcastreceiver onCallEnded (SIP) . There is a brodcastreceiver like this:
public class IncomingCallReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
SipAudioCall incomingCall = null;
try {
SipAudioCall.Listener listener = new SipAudioCall.Listener() {
...
#Override
public void onCallEnded(SipAudioCall call) {
// IncomingCallActivity.finish();
}
};
Main mainActivity = (Main) context;
incomingCall = mainActivity.manager.takeAudioCall(intent, listener);
mainActivity.call = incomingCall;
Intent i = new Intent(context, IncomingCallActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
Then start new activity to answer or reject call, but how to close it when call is finished? Cant use IncomingCallActivity.finish() in onCallEnded.

First why can you add the listener in IncomingCallActivity itself. I dont think that will make any difference.
Second for your solution,
start your activity with a FLAG_ACTIVITY_SINGLE_TOP so that you receive the new Intent in onNewIntent(Intent intent) of the activity.
Then onCallEnded start the activity once again.
Intent i = new Intent(context, IncomingCallActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
i.putExtra("Close", true); // an extra which says to finish the activity.
context.startActivity(i);
Now in onNewIntent(Intent intent)
if(intent.getBooleanExtra("Close", false))
finish();

You can do it like this:
#Override
public void onCallEnded(SipAudioCall call) {
// IncomingCallActivity.finish();
//Here set any value like an int
intToClose = 1;
}
then in your activity check for the intToClose, if its 1 then just call fnish();
UPDATE: This code is from my mind, so it can have some errors. Add this to your current activity, or something like this:
scheduler = Executors.newSingleThreadScheduledExecutor ( );
scheduler.scheduleAtFixedRate ( new Runnable ( ) {
public void run ( )
{
runOnUiThread ( new Runnable ( ) {
public void run ( ) {
closeInt = IncomingCallReceiver.intToClose;
if ( closeInt == 1 ) {
scheduler.shutdown ( );
finish ( );
}
}
} );
}
}, 750, 750, TimeUnit.MILLISECONDS );

I used this code my problem solved.
Try this your problem will be solve. there is no endcall in broadcast so finish() on Idle state.
Happy coding!!
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
((Activity)context).finish();
}

Related

Call a method from other Activity in AlarmReceiver

I'm on a activity with a textview and in the background is AlarmReceiver running with a 10min time trigger.
The activity has a method which I want to call by the trigger. The method sets a new value to the textview. But by calling it from the trigger I can not use "findViewById". I get a NullPointerException at this point. I also tried to setContentView when its called by the trigger but also here I get an NullPointerException
This it the Code:
ValuesActivity:
public void setSyncValue(Context context, boolean fromSyncService, String value){
try {
if(fromSyncService){
setContentView(R.layout.activity_values);
}
...
try{
...
TextView lastSyncTV = (TextView) findViewById(R.id.last_sync_label);
lastSyncTV.setText(value);
...
} catch (Exception ex) {
....
}
} catch (Exception ex) {
....
}
}
AlarmReceiver
#Override
public void onReceive(Context context, Intent intent) {
try {
Intent i = new Intent(context, SyncService.class);
context.startService(i);
}catch (Exception ex){
.....
}
}
SyncService
//runns every 10min
#Override
protected void onHandleIntent(Intent intent) {
try {
...
ValuesActivity valuesActivity = new ValuesActivity();
valuesActivity.setSyncValue(....)
....
}
.....
}
actually you can't create an activity by creating an instance of that, you must run an activity by Intents. in your case if an activity is already in foreground and visible to user you must start activity with a FLAG_ACTIVITY_CLEAR_TOP flag. that send new intent to your current visible activity. to do this you can use this code:
context.startActivity(new Intent(this,ValuesActivity.class).putExtras(bundle).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
in this way if activity already visible to user then android instead of recreating activity just send a new intent to method onNewIntent and you must override this method in your ValuesActivity to receive new intent sent by your service. something like this:
public void onNewIntent(Intent intent){
String value = intent.getStringExtra(/*your key*/);
lastSyncTV.setText(value);
}
if you want to check activity visibility you can add static property in your activity public static boolean isVisible = false; and toggle it true in onResume of your activity and toggle to false in onPause method.
then before startActivity just check if(ValuesActivity.isVisible).
Inside your
#Override
public void onReceive(Context context, Intent intent)
use
Intent intent = new Intent("alaram_received");
context.sendBroadcast(intent);
Inside your mainactivity first register the broadcast
IntentFilter intentFilter = new IntentFilter("alaram_received");
registerReceiver(alarm_receiver,intentFilter);
and then call your method in here:
BroadcastReceiver alarm_receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// your logic here
Log.i("alarm_received","success");
}
};

Android fragment activity not receiving ACTION_SCREEN_OFF or ACTION_SCREEN_ON even

I think I may be missing some little detail and will feel very stupid once this is solved but....
I have many activities that all extend a base class. In the base class there is a Broadcast receiver to catch ACTION_SCREEN_OFF and ACTION_SCREEN_ON.
This will log out the user and return to the main screen.
public class ScreenBReceiver extends BroadcastReceiver
{
public void autoLogout(Context context)
{
MainActivity ma = new MainActivity();
ma.setLoggedIn(false);
Intent homeIntent = new Intent(context, MainActivity.class);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
FDFragmentActivity.this.finish();
context.startActivity(homeIntent);
}
#Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF) || intent.getAction().equals(Intent.ACTION_SCREEN_ON));
{
this.autoLogout(context);
}
}
}
public ScreenBReceiver screenLogout;
However, there is one (1) activity that is not receiving the action.
I cannot for the life of me figure out why the one activity is not receiving this action.
I don't know what else to post. If more info is needed please say so.
All of the activities in the manifest are declared identical.
Neither the xml layout nor the activity code specify that the screen be kept on.
What else could there be?
EDIT:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mContext = this;
mHandler = new Handler();
pi = new PollInterface(mContext);
screenLogout = new ScreenBReceiver();
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
IntentFilter filter2 = new IntentFilter(Intent.ACTION_SCREEN_ON);
registerReceiver(screenLogout, filter);
registerReceiver(screenLogout, filter2);
.......
#Override
public void onDestroy()
{
super.onDestroy();
try
{
unregisterReceiver(screenLogout);
}
catch (IllegalArgumentException iae)
{
// Android can't just let it pass that a receiver was already unregistered.
// So, just ignore the daft exception it throws when unregistering twice.
}
}

Calling Service in Broadcast receiver Issue

When i call my service in a broadcast receiver if i put at the last of onReceive method; the service got triggered correctly but if i put the call and i do some other things after the service never got triggered.
Let me explain, if i do this the service got triggered
public class receiver extends BroadcastReceiver {
#Override
public void onReceive( Context context, Intent intent ) {
// OK
Intent i = new Intent( context, MyService.class );
context.startService( i );
}
}
But if i do this, the service never got triggered and i need to do some other things after the service...
public class receiver extends BroadcastReceiver {
#Override
public void onReceive( Context context, Intent intent ) {
Intent i = new Intent( context, MyService.class );
context.startService( i );
someMethod();
}
}
Maybe i need to add some instruction before i call the service ? .- Sorry about my english
But if i do this, the service never got triggered and i need to do
some other things after the service...
it may help you:
public class receiver extends BroadcastReceiver {
#Override
public void onReceive( Context context, Intent intent ) {
final Context ctx = context;
Thread thread = new Thread(new Runnable(){
#Override
public void run(){
Intent i = new Intent( context, MyService.class );
ctx.startService( i );
}
});
thread.start();
someMethod();
}
}

How to Start same activity with different data from Broadcast Receiver?

I have 1 activity that I would like to start at different times with different variables from a Broadcast Receiver.
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equalsIgnoreCase("tv.abcd.v4.ORIGINAL_VIDEO_SCENE")){
channelName = intent.getExtras().getString("com.abcd.Channel");
JSONObject json = new JSONObject(intent.getExtras().getString("com.abcd.Data"));
String incomingScene = json.getString("url");
scene.putExtra("channel", channelName);
scene.putExtra("url", incomingScene);
scene.addFlags(Intent.FLAG_FROM_BACKGROUND);
scene.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
scene.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(scene);
}
I have the code to start the activity via Intent and in the activity receiver the extras to make data appear.
Intent intent = getIntent();
sceneUrl = intent.getStringExtra("url");
Log.d("Image.incomingscene.url",sceneUrl);
channelName = intent.getStringExtra("channel");
Log.d("Image.incomingSceneAvatar",networkAvatar);
image = (ImageView)findViewById(R.id.imageView1);
image.setScaleType(ScaleType.FIT_CENTER);
progressBar = (ProgressBar)findViewById(R.id.progressBar1);
Picasso.with(this).load(sceneUrl).skipMemoryCache().fit().into(image, new EmptyCallback() {
});
Now after that I want to start the same activity again from the Broadcast Reciever with different data. So i want the previous activity to get out the way and allow this new instance to start up.
How to accomplish this feat?
register another broadcast receiver from the activity. Then, when you want to kill it, send a broadcast message from the broadcast receiver that you mentioned .
In your broadcastReceiver do something like the following :
public static final String CLOSE_Activity= "com.mypackage.closeactivity";
and in yopr OnReceive method do like the following :
#Override
public void onReceive(Context context, Intent intent) {
System.out.println("HIT OUTGOING");
Intent i = new Intent();
i.setAction(CLOSE_Activity);
context.sendBroadcast(i);
}
then in your activity craete a receviver and register it in the onResume method and unregeister it in the onPause method , like the following :
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(RECEIVER_Class.CLOSE_Activity)) {
finish();
}
}
}
activity onResume method :
#Override
public void onResume() {
registerReceiver(broadcastReceiver, new IntentFilter(RECEIVER_Class.CLOSE_Activity));
}
activity onPause method :
#Override
public void onPause() {
super.onPause();
unregisterReceiver(receiver);
}
Please give me some feedback
Hope that helps .

Start IntentService from fragment

I'm trying to start an IntentService from a fragment tab but i have no responce. the code from my fragment is below:
private Intent prepareIntent(boolean isSending) {
Intent localIntent = new Intent(getActivity(), StartIActivity.class);
Log.d(THIS_FILE, "StartIActivity");
localIntent.putExtra("incoming", isSending);
localIntent.putExtra("remote_contact", setValidNumber(callUri));
localIntent.putExtra("acc_id", this.accId);
return localIntent;
}
private void startIAService(boolean bool) {
Log.d(THIS_FILE, "Start Service");
Context ctx = (Context) myFragment.this.getActivity();
ctx.startService(prepareIntent(bool));
return;
}
and my intent serviceclass is :
public class StartIActivity extends IntentService {
public StartIActivity() {
super("StartIActivity");
}
protected void onHandleIntent(Intent it) {
Intent intent = new Intent(it);
intent.setClass(this, Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Log.d("IActivity", "Start Activity");
}
}
When run startIAservice the prepereIntent is run but it can't start the service. I need to use IntentService because i want to execute one task at a time but i can't understand how.
Any help here and what is the best code implemenattion to do this?
Declare service in manifest.xml

Categories

Resources