Issue in calling Activity from the IntentService class - android

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

Related

Proper way to start activity in background

I need to do a logout after some time, so I'm opening the login window in my app using.
startActivity(intent);
Problem is that, if the user has my app in the background, my activity will pop up.
Is there a way to easily open an activity but keep my app in the background?
It can be done with Android appliaction component Service.
you can read about it in official documentation by links below.
https://developer.android.com/training/run-background-service/create-service#java
https://developer.android.com/guide/components/services?hl=en
Initialize your Service
public class MyBackgroundService extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
//onCreate - Service created
}
#Override
public void onDestroy() {
//onDestroy - Service destroyed (Stopped)
}
#Override
public void onStart(Intent intent, int startid) {
//onStart - Service started
}
}
Then call Service in your Main Activity
startService(new Intent(this, MyBackgroundService.class));
And don't forget about declare in Manifest.
<service android:enabled="true"
android:name=".MyBackgroundService" />
You can implement "local logout" after some time and when user returns to activity you can detect it. More: https://developer.android.com/guide/components/activities/activity-lifecycle

Background service work instead the main activity

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;
}
}

How should I implement this secondary thread to avoid errors? (Android)

I am implementing in my Android app a splash screen which:
dowloads a sqlite database from a server
loads urls to get JSONs
creates a sqlite database in the device and execute several queries
I am using AsyncTask to do everything, my problem will occur if the user close the app in the middle of the process or turn off the device because the app:
could be creating a database or executing crucial queries in the device
could be downloading the sqlite db from a server
could be running several important process
etc
Definitely, the entire process (3-5 seconds) is important.
So... How could I avoid this? should I use handlers, loaders, on-(pause, stop, destroy) methods in order to get my objective? Can you give me an example?
As mentioned in the comment above, you should use a service as their lifecycle is separate to that of the activity.
Create the service like so:
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) {
// Do everything you need to here, then call stop:
Log.d("DEBUG", "Started...");
stopSelf();
return super.onStartCommand(intent, flags, startId);
}
#Override
public void onDestroy() {
Intent intent = new Intent("com.example.androidexample.SERVICE_STOPPING");
sendBroadcast(intent);
super.onDestroy();
}
}
Then in the activity:
public class MainActivity extends Activity {
private ServiceCompleteReceiver receiver;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter filter;
receiver = new ServiceCompleteReceiver();
filter = new IntentFilter("com.example.androidexample.SERVICE_STOPPING");
startService(new Intent(this, MyService.class));
registerReceiver(receiver, filter);
}
public class ServiceCompleteReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Do whatever needs to be done here
unregisterReceiver(receiver);
}
}
}
EDIT :
Don't forget to add it to your manifest as well
<service
android:name="com.example.androidexample.MyService"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
</service>

onStartCommand() isn't called when start service in my test case

I have a test project, my simple test case extends AndroidTestCase class :
public class MyTest extends AndroidTestCase{
private Context mContext;
public MyTest(){
super();
}
#Override
public void setUp() throws Exception {
super.setUp();
mContext = getContext();
//Start my service
mContext.startService(new Intent(mContext, MyService.class));
}
#Override
protected void runTest() {
...
}
...
}
In setUp() callback of my above test case, I started MyService.
MyService has also been declared in AndroidManifest.xml of my test project:
<service
android:name="com.my.app.services.MyService"/>
MyService.java :
public class MyService extends Service {
#Override
public void onCreate() {
super.onCreate();
Log.d("MyService", "onCreate()");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Log.d("MyService", "onStartCommand");
}
...
}
But after I run my test case, I noticed from log that neither onCreate() nor onStartCommand() callbacks of MyService have been called.
Why? Is there any special rule applied to Service usage in Android Test Framework which I missed?
The context returned by AndroidTestCase is probably a mocked context - it probably has no implementation for startService. Have you read http://developer.android.com/tools/testing/service_testing.html ?
If you want to wowk with service then use ServicetestCase.
I have been having the same problem, I think. I am trying to test code which calls startService, not test the service itself, so I created a new service for testing purposes.
It appears that test cases can start services, but the services have to be part of the main project being tested, they can't be part of the test project. I don't really like having a test only class in my project, but since it seems to be the only way ...

How to call An activity class from a service class

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.

Categories

Resources