Splash Screen will not display - java

I am trying to implement a Splash Screen using this java and xml code. I created a separate java class from my main activity and placed the java code inside. I created an xml layout in my layout file and placed the xml code inside. However, my app appears normally without a splash screen. It never shows, but the app does not have any errors. Eclipse is not showing any errors either. What could be the cause to this? Thank you in advance. Here is the code.
Java:
package com.carouseldemo.main;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
public class Splash extends Activity {
private final int SPLASH_DISPLAY_LENGHT = 1000;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.splash);
/* New Handler to start the Menu-Activity
* and close this Splash-Screen after some seconds.*/
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent mainIntent = new Intent(Splash.this,Menu.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
}, SPLASH_DISPLAY_LENGHT);
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="#+id/splashscreen" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:src="#drawable/cat"
android:layout_gravity="center"/>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Hello World, splash"/>
</LinearLayout>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.carouseldemo.main"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" android:screenOrientation="portrait"/>
<application android:icon="#drawable/buttonone" android:label="#string/app_name">
<activity android:name=".MainActivity" android:label="#string/app_name" android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

MainActivity is launcher activity in manifest file. So, splash is not showing.
Change the launcher activity MainActivity to Splash and write another for MainActivity.
<application android:icon="#drawable/buttonone" android:label="#string/app_name">
<activity android:name=".Splash" android:label="#string/app_name" android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
</application>

Replace the Handler part with this :
Thread splashThread = new Thread() {
public void run() {
try {
Thread.sleep(SPLASH_DISPLAY_LENGHT);
} catch (Exception e) {
} finally {
Intent i = new Intent(Splash.this,Menu.class);
startActivity(i);
finish();
}
}
};
splashThread.start();

private static final int STOPSPLASH = 0;
private static final long SPLASHTIME =3000;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
context = getApplicationContext();
if(savedInstanceState == null){
Message msg = new Message();
msg.what = STOPSPLASH;
spHandler.sendMessageDelayed(msg, SPLASHTIME);
}
}
private Handler spHandler = new Handler() {
#Override
public void handleMessage(Message msg)
{
switch (msg.what)
{
case STOPSPLASH:
gotoHomeScreen();
break;
default:
break;
}
super.handleMessage(msg);
}
};
public void gotoHomeScreen(){
Intent i = new Intent();
i.setClass(Splash.this,Home.class);
startActivity(i);
finish();
spHandler = null;
}
try this.
in your manifest
<activity android:name="Splash" android:configChanges="keyboardHidden" android:theme="#android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Try out as below :
public class Splash extends Activity
{
private final int SPLASH_DISPLAY_LENGHT = 1000;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
new Thread(){
public void run()
{
try
{
sleep(SPLASH_DISPLAY_LENGHT);
Intent mainIntent = new Intent(Splash.this,Menu.class);
Splash.this.startActivity(mainIntent);
Splash.this.finish();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
};
}.start();
}
EDITED:
<activity android:name=".Splash" android:label="#string/app_name" android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Another way to achieve using CountDownTimer
public class MyCounter extends CountDownTimer {
public MyCounter(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
startActivity(new Intent(SplashScreen.this,
TabSample.class));
finish();
MCObject.cancel();
}
#Override
public void onTick(long millisUntilFinished) {
}
}
Within OnCreate
MyCounter MCObject;
MCObject = new MyCounter(3000, 100);
MCObject.start();

Use this code, it might help you.
public class SplashScreenActivity extends Activity {
protected int _splashTime = 2000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
Thread splashTread = new Thread() {
#Override
public void run() {
try {
sleep(_splashTime);
}
} catch (InterruptedException e) {
// do nothing
} finally {
startActivity(new Intent(SplashScreenActivity.this,
MainActivity.class));
SplashScreenActivity.this.finish();
}
}
};
splashTread.start();
}
Hope it will help you.

Replace your handler
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(SPLASH_TIME);
startActivity(intent);
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();

Related

Service doesnt work correctly in Background

I am making Music Player which downloads music from Firebase.
Its working fine but I cannot play music in background.
It works for like 1.5 minute (after locking screen) and it stops.
In logs after I lock screen it says "Inactivity, disconnecting from Service".
I think this may be main problem here.
Here code :
Fragment of Playing Music:
public class PlayerFragment extends Fragment {
private FirebaseStorage storage;
private ImageButton main_btn, next_btn, back_btn, fav_btn, settings_btn;
private Uri localSong;
private Intent myService;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_player, container, false);
storage = FirebaseStorage.getInstance();
main_btn= view.findViewById(R.id.player_main_btn);
next_btn= view.findViewById(R.id.player_next_btn);
back_btn= view.findViewById(R.id.player_back_btn);
fav_btn = view.findViewById(R.id.player_song_fav_btn);
settings_btn = view.findViewById(R.id.player_song_options_btn);
storage.getReference().child("songs/ni_bien_ni_mal.mp3").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
localSong=uri;
}
});
MainActivity mainActivity = (MainActivity) getActivity();
myService = new Intent(getActivity(), BackgroundSoundService.class);
main_btn.setOnClickListener(v -> {
if( main_btn.getDrawable().getConstantState().equals( main_btn.getContext().getDrawable(R.drawable.ic_play).getConstantState()))
{
new Thread(new Runnable() {
#Override
public void run() {
myService.putExtra("uri",localSong.toString() );
mainActivity.startService(myService);
}
}).start();
main_btn.setImageResource(R.drawable.ic_pause);
}
else
{
new Thread(new Runnable() {
#Override
public void run() {
// mainActivity.stopService(myService);
main_btn.setImageResource(R.drawable.ic_play);
}
}).start();
}
});
fav_btn.setOnClickListener(v->{
if( fav_btn.getDrawable().getConstantState().equals( fav_btn.getContext().getDrawable(R.drawable.ic_heart_empty).getConstantState()))
{
fav_btn.setImageResource(R.drawable.ic_heart_full);
}else{
fav_btn.setImageResource(R.drawable.ic_heart_empty);
}
});
return view;
}
}
Manifest: ( Service declaration is at bottom)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.company.altasnotas">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<meta-data
android:name="preloaded_fonts"
android:resource="#array/preloaded_fonts" />
<application
android:allowBackup="true"
android:icon="#drawable/altas_notes"
android:label="#string/app_name"
android:roundIcon="#drawable/altas_notes"
android:supportsRtl="true"
android:testOnly="false"
android:theme="#style/Theme.AltasNotas">
<activity
android:name=".IntroActivity"
android:label="#string/app_name"
android:theme="#style/Theme.AltasNotas.NoActionBar"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="#string/facebook_app_id"
tools:replace="android:value" />
<meta-data
android:name="preloaded_fonts"
android:resource="#array/preloaded_fonts" />
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="#string/app_name" />
<activity
android:name="com.facebook.CustomTabActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="#string/fb_login_protocol_scheme" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
</activity>
<service android:enabled="true" android:name=".BackgroundSoundService" />
</application>
</manifest>
Service :
public class BackgroundSoundService extends Service {
String song_string;
int x;
private static final String TAG = "BackgroundSoundService";
MediaPlayer player;
public IBinder onBind(Intent arg0) {
Log.i(TAG, "onBind()" );
return null;
}
#Override
public void onCreate() {
super.onCreate();
}
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
public int onStartCommand(Intent intent, int flags, int startId) {
song_string= intent.getStringExtra("uri");
if (song_string != null) {
System.out.println("Song string : " + song_string);
player = MediaPlayer.create(this, Uri.parse(song_string));
player.setLooping(false); // Set looping
player.setVolume(0.5f, 0.5f);
if (!(String.valueOf(x) == null || String.valueOf(x).isEmpty())) {
player.seekTo(x);
}
player.start();
}
return Service.START_STICKY;
}
public IBinder onUnBind(Intent arg0) {
Log.i(TAG, "onUnBind()");
return null;
}
public void onStop() {
Log.i(TAG, "onStop()");
}
public void onPause() {
Log.i(TAG, "onPause()");
x=player.getCurrentPosition();
player.pause();
}
#Override
public void onDestroy() {
if(player!=null) {
player.stop();
player.reset();
player.release();
Log.i(TAG, "onCreate() , service stopped...");
}
}
#Override
public void onLowMemory() {
Log.i(TAG, "onLowMemory()");
}
}
Despite Sticky and declaration in Manifest , it still doesnt work in background. Also I want to add pause button and I dont know exacly how can I do that.

I don't want the app to run the MainActivity while I pressed home in SplashScreen just like WhatsApp

I have put onPause in the SplashScreen activity to override the MainActivity but it won't work.
and I've searched the internet but still not find how to do it. it always start MainActivity even if I've pushed the home button
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
Thread splashScreenThread = new Thread() {
#Override
public void run() {
try {
sleep(2000);
super.run();
} catch (InterruptedException e) {
Log.e(this.getClass().getSimpleName(), "showSplashScreen: " + e.getMessage());
} finally {
Intent moveToHomeActivityIntent = new Intent(SplashScreenActivity.this, HomeActivity.class);
startActivity(moveToHomeActivityIntent);
finish();
}
}
};
splashScreenThread.start();
}
#Override
protected void onPause(){
super.onPause();
finish();
}
Please try with this will working
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(getApplicationContext(), HomeActivity.class));
finish();
}
}, 2000);
Just go to your Manifest.xml and default your activity to SplashScreenActivity
Change This
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SplashScreenActivity" />
to This
<activity android:name=".SplashScreenActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
I've found out the answer! It's so simple and easy to implement. just follow the instruction on this link
Thanks #Zun for providing the reference

Android app in background

I have to create application and want to use it always when phone works, currently my code is:
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.plan.pedometer">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:enabled="true"
android:name=".PedometerService"/>
<receiver
android:name=".receiver.StartPedometerServiceAtBootReceiver"
android:label="StartPedometerServiceAtBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
StartPedometerServiceAtBootReceiver:
public class StartPedometerServiceAtBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent serviceIntent = new Intent(context, PedometerService.class);
context.startService(serviceIntent);
}
}
}
Service:
public class PedometerService extends IntentService {
private int Steps=0;
public PedometerService() {
super("Pedometer_Worker_Thread");
}
#Override
protected void onHandleIntent(Intent intent) {
putInSharedPreferences();
synchronized (this) {
try {
wait(1000000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void putInSharedPreferences(){
stepsNumbers = getApplicationContext().getSharedPreferences("stepsData", MODE_PRIVATE);
editorStepsNumbers=stepsNumbers.edit();
editorStepsNumbers.putInt(String.valueOf(STEPS),Steps).apply();
}
}
It works if I start Service in MainActivity by:
Intent intent = new Intent(getApplication(),PedometerService.class);
startService(intent);
and Service is running in background but when I restart my phone nothing happens and Service does not start.
I have one more question, this is Pedometer application, where should I put onSensorChange method in Service? Create something like while(1) in onHandleIntent() and put it there?
UPDATE - my actually code is:
I followed this tutorial https://github.com/codepath/android_guides/wiki/Starting-Background-Services
and tried to use WakefulBroadcastReceiver
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.plan.pedomoter">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="com.plan.pedomoter.MyTestService"
android:exported="false"/>
<receiver android:name="com.plan.pedomoter.BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
MainActivity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onStart() {
super.onStart();
launchTestService();
}
public void launchTestService() {
// Construct our Intent specifying the Service
Intent i = new Intent(this, MyTestService.class);
// Add extras to the bundle
i.putExtra("foo", "bar");
// Start the service
startService(i);
}
}
Receiver:
public class BootBroadcastReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// Launch the specified service when this message is received
Intent startServiceIntent = new Intent(context, MyTestService.class);
startWakefulService(context, startServiceIntent);
}
}
Service:
public class MyTestService extends IntentService {
Handler handler;
// Must create a default constructor
public MyTestService() {
// Used to name the worker thread, important only for debugging.
super("test-service");
}
#Override
public void onCreate() {
super.onCreate(); // if you override onCreate(), make sure to call super().
// If a Context object is needed, call getApplicationContext() here.
handler = new Handler();
}
#Override
protected void onHandleIntent(Intent intent) {
WakefulBroadcastReceiver.completeWakefulIntent(intent);
handler.post(new Runnable() {
#Override
public void run() {
Toast.makeText(MyTestService.this, "start", Toast.LENGTH_LONG).show();
int i = 0;
}
});
int i=0;
while(true){
i++;
// Do some work here
if(i>1000)
i=0;
}
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(MyTestService.this, "stop", Toast.LENGTH_SHORT).show();
}
}
I used while(true) in MyTestService for check that app is running in background, in project I will use sensors (this is pedometer application) then I want to put something like MyAlarmReceiver from tutorial to send data to the server several times a day.
try this
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mkonuk.rebootapplication">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<service android:name=".MyService"/>
<receiver android:name=".MyBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(getBaseContext(),MyService.class));
}
}
MyService
public class MyService extends Service {
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(getApplicationContext(),"Service started",Toast.LENGTH_LONG).show();
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(getApplicationContext(),"Service Destroyed",Toast.LENGTH_LONG).show();
}
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
MyBootReceiver
public class MyBootReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context,MyService.class);
context.startService(service);
}
}
I have tested when application started service will be start , even if your application destroyed service will be started again.Even if reboot android device service will be started.
Second question :
if you will use sensor with service you need to register sensor listener and unregister sensor listener when service destroyed,oncreate method override sensor object and create sensor and sensor manager.In onstartCommand method register generated sensor listener sensor manager object ,unregister listener when service destroy
you can check this link
Accelemeter in android
if you want to send message from service to activity,you have to implement onbind() method ,activity need to bind on service check this link
bind activity on service using sensor
bind/unbind service
Hello I am a little new and looking for the some help, (It is not an answer) I coulndt add any other commentI was trying to execute a similar app, I followed what mithat show,then when I turn off my mobili it seems like it starts, but just when I put my cellphone password, it appears a system toast that says "app Stop", so i could t get it. Even when I sweep the app from 'Recent app" it says "app Stop"
I would like to run it for ever,
thanks

java.lang.NoClassDefFoundError error in android project

i make a project which includes following jars.
in build path as follows
Main.java
public class Main extends Activity
{
public int welcomeScreenDisplay = 1000;
boolean ispr=false;
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
/** create a thread to show splash up to splash time */
Thread welcomeThread = new Thread() {
int wait = 0;
#Override
public void run() {
try {
super.run();
while (wait < welcomeScreenDisplay) {
sleep(100);
wait += 100;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
Intent mIntent=new Intent(getApplicationContext(), DrawerActivity .class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(mIntent);
finish();
}
}
};
welcomeThread.start();
}
}
DrawerActivity.java
package com.pkg.name;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class DrawerActivity extends FragmentActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner_year);
}
}
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pkg.name"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="11" />
<application
android:allowBackup="true"
android:icon="#drawable/app_icon"
android:label="#string/app_name"
android:theme="#style/CustomActionBarTheme" >
<activity
android:name="com.pkg.name.Splash"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden|stateVisible" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.pkg.name.DrawerActivity"
android:screenOrientation="portrait"
android:theme="#style/AppBaseTheme"
android:windowSoftInputMode="stateHidden|stateVisible" >
</activity>
</application>
</manifest>
When i run my application in device it give me error like
FATAL EXCEPTION: Thread-912
java.lang.NoClassDefFoundError:com.pkg.name.DrawerActivity
at com.pkg.name.Splash$1.run(Splash.java:53)
Any idea how can i solve this ?your all suggestions are appreciable.

A service is not communicating with activity

I'm creating a service which is started, but no communication is received via broadcast. I suspected that there was something wrong with IntentFilter and names, but it all seems correct.
I've been studying example from vogella (par. 8)
What have I done wrong? Why broadcast is not received by MainActivity?
MainActivity
package com.jackslastssid;
import ...
public class MainActivity extends ActionBarActivity {
class MainActivityAfterCaller extends AfterCaller {
public void callback() {
update_something();
}
}
private BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(MainActivity.this, "Daemon called back! :)", Toast.LENGTH_LONG).show(); // never happend
}
};
#Override
protected void onResume() {
super.onResume();
registerReceiver(receiver, new IntentFilter(SayHelloDaemon.NOTIFICATION));
}
#Override
protected void onPause() {
super.onPause();
unregisterReceiver(receiver);
}
}
SayHelloDaemon:
public class SayHelloDaemon extends IntentService {
public static final String NOTIFICATION = "com.jackslastssid.SayHelloDaemon";
public SayHelloDaemon() {
super("SayHelloDaemon");
}
class SayHelloDaemonAfterCaller extends AfterCaller {
public void callback() {
Intent intent = new Intent(NOTIFICATION);
sendBroadcast(intent);
}
}
// will be called asynchronously by Android
#Override
protected void onHandleIntent(Intent intent) {
new SayHelloAsyncTask().execute(new Pair<AfterCaller, Map>(new SayHelloDaemonAfterCaller(), Data.getInstance().getQueryMap()));
}
}
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jackslastssid" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".SayHelloDaemon"
android:icon="#drawable/icon"
android:label="#string/service_name"
android:exported="false"/>
</application>
</manifest>
It looks like you haven't started the service in the activity.
Intent intent = new Intent(this, SayHelloDaemon.class);
startService(intent);

Categories