In my app there is a service class and need to call an activity class from that service class,but it each time while calling the activity class its show me a message that
application is not responding ,and below is my code..
public class MyAlarmService_Movie extends Service {
#Override
public void onCreate() {
super.onStart(intent, startId);
Intent in=new Intent().setClass(MyAlarmService.this,Reminder.class);
startActivity(in);
}
}
Give in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); before starting the intent.
Related
I am have main activity (to-do list application) that also call to background Service and this Service call to some GooglePlacesActivity class. the problem is when the Service is on the application go to the GooglePlacesActivity instead of doing this activity in the background and do the main activity as it should to do.
I will be happy if you can explain to me why this is happened and how I can fix this problem.
( I have two activities and one service. the main activity need to work in the front and the service need to call to the second activity and do the second activity in the background )
Background Service -
public class BackgroundProcess extends Service {
public GooglePlacesActivity PlacesActivity;
#Override
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Let it continue running until it is stopped.
Intent in=new Intent().setClass(BackgroundProcess.this,GooglePlacesActivity.class);
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(in);
return START_STICKY;
}
}
When I have the AlarmReceiver class below in it's own file, the log statement gets logged and the onReceive method gets called. I need to make my BroadcastReceiver an inner class so I can call getFragmentManager() from my Activity. However, when I make it an inner class to my main Activity class, it does not get called. Why is that?
public class MainActivity extends Activity {
public class AlarmReceiver extends BroadcastReceiver { //this needs to be an inner class to access the activity
#Override
public void onReceive(Context arg0, Intent arg1) {
Log.d("BROADCAST","RECEIVED");
//start a new activity, an alarm has gone off
AlarmFragment alarmFragment=new AlarmFragment();
// getFragmentManager().beginTransaction().replace(R.id.container, alarmFragment).commit();
}
}
public void setAlarm(){
Intent intentAlarm = new Intent(this, AlarmReceiver.class);
//Get the Alarm Service
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Long currentTime = new GregorianCalendar().getTimeInMillis();
//DEBUG TIME
alarmManager.set(AlarmManager.RTC_WAKEUP,currentTime+7500,PendingIntent.getBroadcast(this,1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
}
}
Kindly change your Activity Class to FragmentActivity and also Access with context Object,like below code.
arg0.getFragmentManager().beginTransaction().replace(R.id.container, alarmFragment).commit();
Its is a Try. Kindly let me know your feedback.
Why is that?
you are got major fundamental mistake about how to send and receive broadcast mechanism works:
you are providing to alarm manager a PendingIntent to start broadcast, but in practice - you providing it intent without any Action specified.
sending broadcast without an action is meaningless!!!
you should add to the intentAlarm object an custom action string that identify your broadcast, and register your AlarmReceiver receiver programatically with an IntentFilter that handles this custom action:
Intent intentAlarm = new Intent(CUSTOM_ACTION_STRING);
....
....
(you should hold reference to an instance of your AlarmReceiver class. that's the mAlarmReceiver ...)
add to the Activity onResume() callback registration to the broadcast:
#Override
public void onResume() {
super.onResume();
registerReceiver(mAlarmReceiver , new IntentFilter(CUSTOM_ACTION_STRING));
}
don't forget also to unregister the receiver:
#Override
public void onPause() {
unregisterReceiver(mAlaramReceiver);
super.onPause();
}
I have two activities: one is the main (A), and the second one which is started from A (B). I start B with startActivityForResult(intent, id).
I know I can send the result back to A via the setResult() method, but as far as I know, the result isn't sent until finish() is called. I need to send data from B to A without closing B (even several times before closing). Is there a way to achieve that?
As far as I've read, there are not many options to achieve this. I could use SharedPreferences but then I'd need also some kind of event to inform A that it has to read a value!
Any ideas appreciated.
------ FINAL SOLUTION ------
Finally I got it thanks to #Nathaniel Waggoner's advice. Here's what I did:
Inside my activity I declared the extension of BroadcastReceiver:
class ActivityBroadcast extends BroadcastReceiver {
#Override
public void onReceive(final Context context, final Intent intent) {
final String txt2send = intent.getStringExtra("txt2send");
if ((txt2send != null) && (!txt2send.isEmpty())) {
// Do the needed stuff
...
}
}
}
So now I declared the ActivityBroadcast instance in my class and initialized it:
private static ActivityBroadcast broadcast_signal;
broadcast_signal = new ActivityBroadcast();
The way I control that it's just my Intent the one who triggers the onReceive method is with an IntentFilter set to the SENDTXT2SOCK customized action, this way:
// CustomActions.SENDJOIN2CHAN is just a constant from a customized public class
// where I define my own constants to not interfere with the "official" ones
registerReceiver(broadcast_signal, new IntentFilter(CustomActions.SENDTXT2SOCK));
This way I'm saying that on broadcast_signal will just be registered the CustomActions.SENDTXT2SOCK action, so any other is ignored. Now we just have to send a signal from the desired activity to that receiver:
final Intent intentResult = new Intent(CustomActions.SENDTXT2SOCK);
intentResult.putExtra("txt2send", "blabla");
sendBroadcast(intentResult);
And that's all, works like a charm!
Use broadcasts and intents.
Broadcast Receivers:
http://developer.android.com/reference/android/content/BroadcastReceiver.html
You can also give a shot to OnsharedPreferenceChangelistner
you can use eventBus simple library.
first register eventbus in receiver activity
#Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
then set a subscriber method
#Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MessageEvent event) {/* Do something */};
then in other activity post your entity like this
EventBus.getDefault().post(new MessageEvent());
You can use broadcasts and observers to call a method of one activity from another activity. You will use an intent to call a broadcast from activity B, and then you will use an observer to call a method in activity A from the broadcast.
For example, if you want to call ActivityA.someMethod from ActivityB, you can do the following (don't forget implements Observer on ActivityA):
public class ActivityA extends AppCompatActivity implements Observer{
//This is the method you want to call from ActivityB
public void someMethod(Intent intent){
//intent will be the Intent object created in ActivityB, you can pass data to this method by setting extras in the intent
}
//Define the observer and broadcast receiver classes
private static class MyObservable extends Observable{
private static final MyObservable instance = new MyObservable();
public void updateValue(Object data){
synchronized(this){
setChanged();
notifyObservers(data); //This method calls ActivityA.update
}
}
}
public static class MyBroadcastReceiver extends BroadcastReceiver{
//This class must be public and static and must be added to the manifest
//To add this class to the manifest in Android Studio put the cursor on the name of the class, press Alt+Enter and choose "Add to manifest"
#Override
public void onReceive(Context context, Intent intent){ //This method will be called when the broadcast is sent from ActivityB
MyObservable.instance.updateValue(intent);
}
}
//Connect the observer to the activity in the onCreate method
#Override
protected void onCreate(Bundle savedInstanceState){
MyObservable.instance.addObserver(this);
//Do everything else in onCreate as usual
}
//This method will be called when the notifyObservers method is called from the oberver
#Override
public void update(Observable observable, Object data){
this.someMethod((Intent) data); //Call the method that you need
}
}
public class ActivityB extends AppCompatActivity{
public void callSomeMethodOnActivityB(){
Intent intent = new Intent(this, ActivityA.MyBroadcastReceiver.class);
//Add some extras to the intent to pass data
sendBroadcast(intent); //Calls ActivityA.MyBroadcastReceiver.onReceive
}
}
Most of this answer is based on ilw's answer to "Communication between BroadcastReceiver and Activity", credits to them.
I want to call a custom method in a class which can be passed in Intent. In the receiver side of Intent, I want to call my custom method of the class. Lets say I have a class extends ResultReceiver which two methods
class MyClass extends ResultReceiver {
public doBefore(){ //custom method
Log.d("sdf","before");
}
public doAfter(){ // custom method
Log.d("sdf","After");
}
#Override
public void onReceiveResult(final int resultCode, final Bundle resultData) {
}
}
I want to pass the MyClass in the Intent to another Activity or Service. So Lets say i am passing the MyClass to a service
MyClass mcl = new MyClass()
final Intent intent = new Intent(mContext, MyService.class);
intent.putExtra(INTENT_EXTRA_RECEIVER, mcl);
In MyService class, I get the intent in onHandleIntent() method.
Method in MyService Class
#Override
protected void onHandleIntent(Intent intent) {
MyClass eval = (MyClass) intent.getParcelableExtra(INTENT_EXTRA_RECEIVER);
eval.doBefore(); // Is this possible??
eval.doAfter();
sendSuccess(intent, null);
}
Now I want to execute the two methods in the of the class "MyClass". onReceiveResult() in the "MyClass" is called at the end but I am not able to call my custom method. I dont want to start an activity or service. I want to my custom method to be executed.
Is there anyway I can call my custom method from the Service or Activity like ResultReceiver's onReceiveResult().??
I am not sure if I understand your question properly, because it seem confusing that you try to access through your intent calls. Would it not be possible to simply do like below?
mcl.doBefore();
In my app, I am using the IntentService class to start another activity in the background. But the issue I got is that suppose from the IntentService class I start my activity, which opens my activity, after that I don't close my activity. Then I notice that when IntentService class again wants to start my same activity it is not called as the same activity is not close.
So, my question is: How can I start the same activity again and again whether it is open or close from the IntentService class?
Code in IntentService class
public class AlarmService extends IntentService
{
public void onCreate() {
super.onCreate();
}
public AlarmService() {
super("MyAlarmService");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, startId, startId);
return START_STICKY;
}
#Override
protected void onHandleIntent(Intent intent) {
startActivity(new Intent(this,
AlarmDialogActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}
}
use launchMode tag in manifest file
<activity
android:name=".ActivityName"
android:launchMode="singleTask" />
it will not create a different instance of activity if already available..
see this link launchMode for better understanding