How do I keep Background Service always run in background - java

I am newbie to android studio and learning about services, I visited this page : https://xjaphx.wordpress.com/2012/07/07/create-a-service-that-does-a-schedule-task/
In which author made a background service as follows:
files go like:
So I create my own service called TimeService:
public class TimeService extends Service {
// constant
public static final long NOTIFY_INTERVAL = 10 * 1000; // 10 seconds
// run on another Thread to avoid crash
private Handler mHandler = new Handler();
// timer handling
private Timer mTimer = null;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
// cancel if already existed
if(mTimer != null) {
mTimer.cancel();
} else {
// recreate new
mTimer = new Timer();
}
// schedule task
mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL);
}
class TimeDisplayTimerTask extends TimerTask {
#Override
public void run() {
// run on another thread
mHandler.post(new Runnable() {
#Override
public void run() {
// display toast
Toast.makeText(getApplicationContext(), getDateTime(),
Toast.LENGTH_SHORT).show();
}
});
}
private String getDateTime() {
// get date time in custom format
SimpleDateFormat sdf = new SimpleDateFormat("[yyyy/MM/dd - HH:mm:ss]");
return sdf.format(new Date());
}
}
Andoid Manfest.xml
<?xml version="1.0" encoding="utf-8"?>
<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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".TimeService"
android:enabled="true"
android:exported="true"></service>
</application>
MainActivity.java is :
package com.example.shubhamrajput.myapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(this, TimeService.class));
}
}
I tested this app on my phone ,but it is not working in the background when I terminate it from app tray, I want to make it run forever until user stops it forcefully from settings, How do I change this code? Please provide detailed explanation and also provide modified code ,so that I can understand it.How can I make it a foreground service?

You should not be running service in the background always because of it will use CPU and memory all the time.You will end up having very poor battery backup
You can use Job Scheduler for API level greater than 21 or Firebase Job Dispatcher for below API level 21.Using this, you can fire recurring job in an efficient manner.

You can run the service on different process so it will run always irrespective of the application
In manifest file
<service
android:name=".TimeService"
android:enabled="true"
android:process=":my_process">
</service>
You can also use START_STICKY
or you can follow this Answer for more details.

Related

Wrong activity displayed when implicit intent received from another app with Action.SEND

I have an app with two activities: MainActivity, which contains a URL entry field where the user can enter a YouTube video URL and press a submit button, to start the second activity, VideoActivity, which displays some information about this video (fetched from another web server).
The app also has a feature to receive intent via the Youtube application. When user presses the share button within the Youtube app, my app appears in the share list. Upon pressing share from the Youtube app, MainActivity should be brought to the front, and the URL should be posted within the MainActivity's URL field.
However, this only happens correctly on the first share. If the app is in the background when user shares from Youtube app, they are taken to whatever the last visible activity was, whether it is MainActivity or VideoActivity, (and even if it is MainActivity, the URL is not posted into the URL field, but the field is left in whatever state it was in when the app was last visible).
Here is my current AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.youcmt.youdmcapp">
<uses-permission android:name="android.permission.INTERNET"/>
<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:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>
<activity
android:name=".VideoActivity"
android:parentActivityName=".MainActivity"/>
<service
android:name=".FetchVideoService"
android:exported="false"/>
</application>
</manifest>
Here is my MainActivity.java code:
public class MainActivity extends AppCompatActivity {
private ResponseReceiver mReceiver;
private EditText mUrlEditText;
private Button mSearchButton;
private ProgressBar mProgressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
mUrlEditText = findViewById(R.id.url_search_et);
Intent intent = getIntent();
if (intent.getType()!=null &&
intent.getType().equals("text/plain")) {
Bundle extras = getIntent().getExtras();
String value = extras.getString(Intent.EXTRA_TEXT);
if(value!=null)
{
mUrlEditText.setText(value);
}
}
mProgressBar = findViewById(R.id.progress_bar);
mSearchButton = findViewById(R.id.search_button);
mSearchButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
askForVideo(mUrlEditText.getText().toString());
mSearchButton.setVisibility(View.INVISIBLE);
mProgressBar.setVisibility(View.VISIBLE);
} catch (Exception e) {
mUrlEditText.setText("");
mUrlEditText.setHint(e.getMessage());
e.printStackTrace();
}
}
});
}
#Override
protected void onResume() {
super.onResume();
//register the ResponseReceiver
mReceiver = new ResponseReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(FETCH_VIDEO_INFO);
registerReceiver(mReceiver, intentFilter);
}
private void askForVideo (String url) throws Exception {
try {
Intent intent = FetchVideoService.newIntent(this, url);
startService(intent);
} catch (Exception e) {
mUrlEditText.setText(e.getMessage());
}
}
public class ResponseReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
int status = intent.getIntExtra(EXTRA_VIDEO_STATUS, FAIL);
mProgressBar.setVisibility(View.INVISIBLE);
mSearchButton.setVisibility(View.VISIBLE);
if(status==FAIL)
{
mUrlEditText.setText("");
mUrlEditText.setHint("Error retrieving video!");
}
else if(status==SUCCESS) {
Video video = intent.getParcelableExtra(EXTRA_VIDEO);
Intent videoActivityIntent =
VideoActivity.newIntent(getApplicationContext(), video);
startActivity(videoActivityIntent);
}
}
}
#Override
protected void onPause() {
unregisterReceiver(mReceiver);
super.onPause();
}
}
I do not think any of the other files will be useful in understanding the problem. Although this seems like something many app creators should have to deal with, I can find no answers to this problem. Please comment if you feel I should add any additional information and thank you in advance for any help!
Update: testing demonstrates that after the first use of "Share" from YouTube (and considering app remains in the background), the MainActivity no longer receives any new intent on further shares. However, my app is still brought to the foreground somehow. This is very confusing to me.
When you share from another app, your MainActivity is brought to the front and onNewIntent() is called on it. You don't override onNewIntent() so you never see the share Intent.

How to stop JobService Scheduled to when app is removed from background?

Currently i am working with an application and my app has a feature that the user will be able to click on a Navigate button and my app will start the Google Map. Till now it's fine and i have done it. But the fact where i am stuck is that i want my app to perform some tasks. To achieve that i have used JobService and scheduled it to run after every 5 seconds even when the app is in background.
When the user presses the back button then inside onDestroy method i have cancelled the scheduler. But when the app is removed from the background by sliding or pressing the cross icon the JobService keeps running as the onDestroy method can be called or not by the os when it is removed from the background. How can i stop the scheduled job when the app is removed from the background?
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="javarank.com.serviceinbackground">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyJobService" android:exported="true" android:permission="android.permission.BIND_JOB_SERVICE" />
</application>
</manifest>
MyJobService class
public class MyJobService extends JobService {
#Override
public boolean onStartJob(final JobParameters jobParameters) {
Toast.makeText(getApplicationContext(), "Doing job", Toast.LENGTH_SHORT).show();
jobFinished(jobParameters, true);
return false;
}
#Override
public boolean onStopJob(JobParameters jobParameters) {
return false;
}
}
Here is my MainActivity
public class MainActivity extends AppCompatActivity {
private static final int JOB_ID = 1;
private JobInfo jobInfo;
private JobScheduler scheduler;
private Button navigateButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ComponentName componentName = new ComponentName(this, MyJobService.class);
JobInfo.Builder builder = new JobInfo.Builder(JOB_ID, componentName);
builder.setPeriodic(5000);
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
// if true this job exists even after a system reboot...
builder.setPersisted(false);
jobInfo = builder.build();
scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
scheduler.schedule(jobInfo);
navigateButton = (Button) findViewById(R.id.navigate_button);
navigateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
StringBuffer url = new StringBuffer("https://www.google.com/maps/dir/?api=1");
url.append("&origin=23.755736,90.374627");
url.append("&destination=23.754047,90.371682");
url.append("&travelmode=driving");
Uri gmmIntentUri = Uri.parse(url.toString());
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
startActivity(mapIntent);
}
});
}
#Override
protected void onDestroy() {
Toast.makeText(getApplicationContext(), "Destroy called.", Toast.LENGTH_SHORT).show();
scheduler.cancel(JOB_ID);
super.onDestroy();
}
}
I think you need to override following onStop() method and put stopService() command to stop the JobService.
#Override
protected void onStop() {
// A service can be "started" and/or "bound". In this case, it's "started" by this Activity
// and "bound" to the JobScheduler (also called "Scheduled" by the JobScheduler). This call
// to stopService() won't prevent scheduled jobs to be processed. However, failing
// to call stopService() would keep it alive indefinitely.
stopService(new Intent(this, MyJobService.class));
super.onStop();
}
You can create a new service like
MyService.java
public class MyService extends Service {
public MyService() {
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
//stop you jobservice from here
stopSelf();
}
}
and start it from MainActivity.java
startService(new Intent(MainActivity.this,MyService.class));
Android> 7 automatically saves battery power. You must turn on the application's battery saving stop feature.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent intent = new Intent();
String packageName = getPackageName();
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
if (!pm.isIgnoringBatteryOptimizations(packageName)) {
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
startActivity(intent);
}
}
add this to AndroidManifest.xml
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
I faced this issue, but I found that after schedule job service, it can't be canceled (From view).
So I turned to stop it inside the job service by calling onStopJob(params)
and it worked.

Android background service works slower when phone in sleep mode or app hidden for a some long time

I write an android native app that asks for LatLng coordinate periodically.
So, I have MainActivity class, where the UI runs. And here I initiate a background service, using Intent:
public class MainActivity extends AppCompatActivity {
// here are defined required methods like OnCreate() etc.
Intent locTaskIntent = new Intent(this, LocationsTaskService.class);
startService(locTaskIntent);
// ...
}
A Service, being initiated by onStartCommand(), schedules asynchronous tasks periodically:
public class LocationsTaskService extends Service {
public int onStartCommand(Intent intent, int flags, int startid) {
final Handler handler = new Handler();
Timer taskTimer = new Timer();
TimerTask asyncTask = new TimerTask() {
#Override
public void run () {
handler.post(new Runnable() {
public void run () {
LocationProcessor LocationProcessor = new LocationProcessor(mContext, ++counter);
LocationProcessor.startRoutine();
}
});
}
};
taskTimer.schedule(asyncTask, 0, RConstants.locUpdateInterval);
return super.onStartCommand(intent, flags, startid);
}
Where the startRoutine() method of LocationProcessor class performs operations for android os to request locations, and then waits for an answer in callback method, implemented in LocationListener or cancels the search by timeout:
private class LocationProcessor {
// ....
LocationProcessor(Context context, int sessionId) {
mLocManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
mSessionId = sessionId;
}
public void startRoutine() {
// The CountDownTimer is used to break locations search by timeout
cdTimer = new CountDownTimer(RConstants.locUpdateTimeoutUsed, RConstants.locUpdateTimeoutUsed ) {
#Override
public void onFinish() {
// cancel updates listening
mLocManager.removeUpdates(locationListener);
mLocManager = null;
}
}.start();
// requesting for location updates
mLocManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDTS,
MIN_DIST_CHANGE_FOR_UPDTS,
locationListener
);
}
private LocationListener locationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
cdTimer.cancel();
cdTimer = null;
mLocManager.removeUpdates(locationListener);
mLocManager = null;
}
}
And Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.location.devsname.appname">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:launchMode="singleTask">
<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>
<service android:name=".LocationsTaskService">
</service>
</application>
</manifest>
The problems are:
App works fine until the phone fall asleep for a some long time and / or app being hidden.
When I wake the phone, I can see the following picture (red marks are related to state, when phone sleeps, and, for opposite state - green marks, when messages receive promptly):
When I launch some number of memory consuming apps in phone (it has only 512 MB RAM), I receive message from System like: "Sorry, ..the app name.., has stopped". It seems like the OS tends to free memory for new loading apps, killing the old processes - it's my guess. How can I forcibly say to the System to care it running?
For your 2nd point, I believe returning START_STICKY from your onStartCommand will do the trick.
From the documentation
int START_STICKY
Constant to return from onStartCommand(Intent, int, int): if this
service's process is killed while it is started (after returning from
onStartCommand(Intent, int, int)), then leave it in the started state
but don't retain this delivered intent. Later the system will try to
re-create the service. Because it is in the started state, it will
guarantee to call onStartCommand(Intent, int, int) after creating the
new service instance; if there are not any pending start commands to
be delivered to the service, it will be called with a null intent
object, so you must take care to check for this.
This mode makes sense for things that will be explicitly started and stopped to run for arbitrary periods of time, such as a service performing background music playback.

SplashScreen everytime I'm back on MainActivity

I'm using this code on MainActivity for a splashscreen that works perfectly
final ImageView splash1 = (ImageView) this.findViewById(R.id.splash);
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
splash1.setVisibility(View.GONE);
}
}, 1000);
but everytime I'm back on MainActivity (where the main menu is), the splashScreen is there again. Is there a way to keep using this code, and just adding an if condition do not see the splashScreen after the first time?
(e.g: a variable that changes when the app loads)
Thanks in advance
Use 2 different activity SplashActivity and MainActivity.
Your "Splash" activity need to be MAIN LAUNCHER Activity. So modify the AndroidManifest file like this...
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
</activity>
<activity android:name=".Splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
And Jump to MainActivity from SplashActivity after few seconds.. Use this code in SplashActivity.
Handler hadler=new Handler();
hadler.postDelayed(new Runnable() {
#Override
public void run () {
finish();
Intent i = new Intent(context, MainActivity.class);
startActivity(i);
}
}, 3000);
here 3000 is used for 3 seconds. The MainActivity auto start after 3 seconds. Hope it helps.
Use separate activity for splash screen after that go for MainActivity, Don't forget to use finish() in splash screen activity.
This link may help you
http://androidexample.com/Splash_screen_-_Android_Example/index.php?view=article_discription&aid=113&aaid=135
Simply create one variable to know whether its displayed or not.
class YourActivity extends Activity {
boolean isDisplayed;
#Override
protected void onStart() {
if (!isDisplayed) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
isDisplayed = true;
splash1.setVisibility(View.GONE);
}
}, 1000);
} else {
splash1.setVisibility(View.GONE);
}
}
}
use finish() after starting the SplashScreen Activity
EDIT:
One more approach can be - Create a boolean application level variable (set to false) by extending Applicationclass & then checking it in the runmethod - If false then show the splash & set it to true, so that it will not execute again.
public class DefaultApplication extends Application {
private boolean isSplashDisplayed = false;
public boolean isSplashDisplayed() {
return isSplashDisplayed ;
}
public void setIsSplashDisplayed(boolean isSplashDisplayed) {
this.isSplashDisplayed = isSplashDisplayed;
}
}
Second Approach -
Its better to create a separate activity for Splash, then call MainActivity from SplashActivity & finish SplashActivity
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
finish();
}
}, 1000);
Also need to make your SplashActivity as launcher
<activity
android:name=".SplashActivity"
android:label="#string/title_activity_splash_screen" >>
<intent-filter>
<action android:name="android.intent.action.MAIN" />>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
< /activity>

Android Splash screen to activity

I am creating an android app and when I go to debug it on my samsung galaxy the Splash activity loads first,as it should, but after that the app crashes/stops right after doing the "Splash" activity. It doesn't go to the "MainActivity" activity after the thread sleeps for 5 seconds. Does anyone know what might be causing the problem? Plus after I tried debugging the app and loaded it onto my phone the app isn't even showing up. I am using Eclipse by the way. It shows the app in my application manager on my phone but it doesn't show the icon in my app screen.
Here is my Splash.java:
package com.example.mihirsandroidapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
} catch (InterruptedException e){
e.printStackTrace();
}finally{
Intent openMainActivity = new Intent("com.example.mihirandroidsapp.MAINACTIVITY");
startActivity(openMainActivity);
}
}
};
timer.start();
}
#Override
protected void onPause() {
super.onPause();
finish();
}
}
Here is my Manifest:
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:debuggable="true"
android:allowBackup="true"
android:icon="#drawable/cartooncat"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Splash"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.mihirsandroidapp.SPLASH" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.mihirsandroidapp.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
And here is my main activity which should start after the splash screen:
package com.example.mihirsandroidapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int counter;
Button add, sub;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
add = (Button) findViewById(R.id.bAdd);
sub = (Button) findViewById(R.id.bSub);
display = (TextView) findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter += 1;
display.setText("Total is " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter -= 1;
display.setText("Total is " + counter);
}
});
}
}
Oh.. Where do I start.. Let's go through all of the issues:
1) Fix your manifest. Definitely not the right way to declare your activities. Here is what it should look like:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:debuggable="true"
android:allowBackup="true"
android:icon="#drawable/cartooncat"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Splash"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
</activity>
</application>
2) Now let's fix the way you start your activity:
Intent openMainActivity = new Intent(Splash.this, MainActivity.class);
3) Don't call finish() in onPause() - you break native activity lifecycle flow. Call finish() right after you start new activity:
Intent openMainActivity = new Intent(Splash.this, MainActivity.class);
startActivity(openMainActivity);
finish();
4) Instead of creating separate thread, just a create a Handler and post Runnable there with 5 seconds delay:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//this will be called after 5 seconds delay
}
}, 5000);
Here is entire file put together:
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent openMainActivity = new Intent(Splash.this, MainActivity.class);
startActivity(openMainActivity);
finish();
}
}, 5000);
}
If it doesn't help - we definitely need to look at logcat output...
A simple way to achieve this if one is ready to compromise on drawing performance is to define custom theme with splash image that one wants to use as window background and use this custom theme as application theme
styles.xml
<resources>
<style name="CustomTheme" parent="android:Theme">
<item name="android:windowBackground">#drawable/background_image</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
AndroidManifest.xml
<application
android:debuggable="true"
android:icon="#drawable/icon"
android:theme="#style/CustomTheme"
android:label="#string/app_name">
...
</application>
This would use the #drawable/background_image as the window.background. As a result if the activities has transparent background then #drawable/background_image will be visible as activities background. One can avoid this by setting appropriate color or drawable in onCreate of every activity programatically as
public void onCreate(){
super.onCreate(savedInstanceState);
setContentView(R.layout.layoutResID);
activity.getWindow().setBackgroundDrawableResource(R.color.window_bg);
}
Check this for more information
All what you need for a splash screen
SplashActivity.java
public class SplashActivity extends AppCompatActivity {
private final int SPLASH_DISPLAY_DURATION = 1000;
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
Intent mainIntent = new Intent(SplashActivity.this,MainActivity.class);
SplashActivity.this.startActivity(mainIntent);
SplashActivity.this.finish();
}
}, SPLASH_DISPLAY_DURATION);
}}
In drawables create this bg_splash.xml
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#color/app_color"/>
<item>
<bitmap
android:gravity="center"
android:src="#drawable/ic_in_app_logo_big"/>
</item></layer-list>
In styles.xml create a custom theme
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/bg_splash</item>
</style>
and finally in AndroidManifest.xml specify the theme to your activity
<activity
android:name=".activities.SplashActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Cheers.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread th = new Thread(new Runnable() { /*create a new thread */
#Override
public void run() { /*
* The purpose of this thread is to
* navigate from one class to another
* after some time
*/
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
/*
* We are creating this new thread because we don’t
* want our main thread to stop working for that
* time as our android stop working and some time
* application will crashes
*/
e.printStackTrace();
}
finally {
Intent i = new Intent(MainActivity.this,
Splash_Class.class);
startActivity(i);
finish();
}
}
});
th.start(); // start the thread
}
http://www.codehubb.com/android_splash_screen
I have added splash screen by using following code:
public class SplashActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_layout);
initConstatnts();// method for intilizing any constants
new Thread(new Runnable() {
#Override
public void run() {
if (!isFinishing()) // checking activity is finishing or not
{
try {
Thread.sleep(3000);//delay
Intent i = new Intent(getBaseContext(),
HomeActivity.class);
startActivity(i);
finish();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}).start();
}
private void initConstatnts() {
}
}
Flow the below steps to Create your own splash screen
Create Activity1 and XML file
Design the XML file and put the welcome message
Crete Activity2 and XML file.
Start the Activity1
Start a Thread in Activity1 and sleep for 5 seconds
Start Activity2 from the Thread
There is nothing called readymade splash screen in Android . we can achieve that using above steps.
In a single line, start Activity1 wait for 5 sec then start Activity2.
So user will feel that first screen is splash screen.
You can download the complete code from below link
http://javaant.com/splash-screen-android/#.VwzHz5N96Hs
i have faced the same problem.... i think you code is perfect for the app
u just try with this in the Intent Creation in your splash activity class
Intent openMainActivity = new Intent("android.intent.action.MAIN");//MAIN is the that u want to start
//next after the current Activity

Categories

Resources