Not able to change application's launching activity - Android - android

I am not able to change starting activity of application. In starting my starting activity was com.example.image_changer.MainActivity and with this my application run correctly. Then I change my launching activity from MainActivity to com.example.image_changer.Splash. But my application not launching com.example.image_changer.Splash activity.I want com.example.image_changer.Splash as starting activity of my application.
I have tried these solutions:
1. In eclipse, I change this setting: Run menu-->Debug Configuration---->Under my app--->Android tab--->Launch Action---->Launch(radio button)---->select(from drop down menu)--->com.example.image_changer.Splash.
2. Internet search:
I have tried all solution given on this link: Change application's starting activity
In this link zeh (user) post comment, which is possibly look like same as my problem but nobody post better solution regarding this.
Note-When I run my program then it runs splash.java but not show on emulator screen, I know this because I code System.out.println(); in thread in splash.java and it print string every time in console when I run my app.
So how to solve this problem?
This is my manifest:
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/imagechanger"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.image_changer.Splash"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.image_changer.MainActivity1"></activity>
<activity android:name="com.example.image_changer.GamesFragment"></activity>
<activity android:name="com.example.image_changer.MoviesFragment"></activity>
<activity android:name="com.example.image_changer.TabsPagerAdapter"></activity>
<activity android:name="com.example.image_changer.TopRatedFragment"></activity>
<activity android:name="com.example.image_changer.Imageswipe"></activity>
<activity android:name="com.example.image_changer.Mapview"></activity>
<activity
android:name="com.example.image_changer.MainActivity"
android:label="#string/app_name"></activity>
</application>
</manifest>
This is my Splash.java class file
package com.example.image_changer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
public class Splash extends Activity {
/** Called when the activity is first created. */
private Thread mSplashThread;
/** Called when the activity is first created. */
#Override
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.activity_splash);
//setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
final Splash sPlashScreen = this;
// The thread to wait for splash screen events
mSplashThread = new Thread(){
#Override
public void run(){
try {
synchronized(this){
// Wait given period of time or exit on touch
wait(2500);
}
}
catch(InterruptedException ex){
}
// Run next activity
Intent intent = new Intent();
intent.setClass(sPlashScreen,MainActivity.class);
System.out.println("your are in the intent");
startActivity(intent);
finish();
}
};
mSplashThread.start();
}
/**
* Processes splash screen touch events
*/
#Override
public boolean onTouchEvent(MotionEvent evt)
{
if(evt.getAction() == MotionEvent.ACTION_DOWN)
{
synchronized(mSplashThread){
mSplashThread.notifyAll();
}
}
return true;
}
}

Update your thread code to this.
new Handler().postDelayed(new Runnable() {
public void run() {
// Run next activity
Intent intent = new Intent();
intent.setClass(sPlashScreen,MainActivity.class);
System.out.println("your are in the intent");
startActivity(intent);
finish();
}
}, 2500);

Related

activity does not open on event detection

I am following this answer to implement app un-install detector. I am basically trying to open Main Activity when un-installation event is detected, but when I un-istall my app nothing happens (Main Activity does not open before un-install).
Please help, what is wrong with my code?
MainAcivity:
package com.example.appdeveloper.unistalltest;
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);
}
}
Broadcast Receiver:
package com.example.appdeveloper.unistalltest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class UninstallIntentReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// fetching package names from extras
String[] packageNames = intent.getStringArrayExtra("android.intent.extra.PACKAGES");
if(packageNames!=null){
for(String packageName: packageNames){
if(packageName!=null && packageName.equals("com.example.appdeveloper.unistalltest")){
// User has selected our application under the Manage Apps settings
// now initiating background thread to watch for activity
new ListenActivities(context).start();
}
}
}
else {
Toast.makeText(context, "No package found in Broadcast Receiver", Toast.LENGTH_LONG).show();
}
}
}
ListenActivities.java:
package com.example.appdeveloper.unistalltest;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;
import java.util.List;
public class ListenActivities extends Thread {
boolean exit = false;
ActivityManager am = null;
Context context = null;
public ListenActivities(Context con){
context = con;
am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
}
public void run(){
Looper.prepare();
while(!exit){
// get the info from the currently running task
List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(MAX_PRIORITY);
String activityName = taskInfo.get(0).topActivity.getClassName();
Log.d("topActivity", "CURRENT Activity ::"
+ activityName);
if (activityName.equals("com.android.packageinstaller.UninstallerActivity")) {
// User has clicked on the Uninstall button under the Manage Apps settings
//do whatever pre-uninstallation task you want to perform here
// show dialogue or start another activity or database operations etc..etc..
context.startActivity(new Intent(context, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
exit = true;
Toast.makeText(context, "Done with preuninstallation tasks... Exiting Now", Toast.LENGTH_LONG).show();
}
else if(activityName.equals("com.android.settings.ManageApplications")) {
// back button was pressed and the user has been taken back to Manage Applications window
// we should close the activity monitoring now
exit=true;
}
}
Looper.loop();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.appdeveloper.unistalltest">
<uses-permission android:name="android.permission.GET_TASKS"/>
<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>
<receiver android:name=".UninstallIntentReceiver">
<intent-filter android:priority="0">
<action android:name="android.intent.action.QUERY_PACKAGE_RESTART" />
<data android:scheme="package" />
</intent-filter>
</receiver>
</application>
</manifest>
Your app can't know when the user uninstall it since Android 4.0+. I had a similar problem, but didn't found a way to do it. The only things I found today (i already gave up with my app, but maybe it can help you) is this: https://developer.android.com/guide/topics/admin/device-admin.html
I'm not sure this will help you, but it is the only thing i found.

"Client not ready yet" when adding splashscreenactivity

I want to add a splashscreen activity to my app.
I'm using Android Studio 2.2, Preview 3
I just modify my manifest, to add in the new activity :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="me.youactive.hts.youactive">
<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"
android:label="YouactiveSlidingMenu"
android:screenOrientation="portrait">
</activity>
<activity
android:name=".SplashScreenActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
and here is my SplashScreenActivity.java
package me.youactive.hts.youactive;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class SplashScreenActivity extends AppCompatActivity
{
private final int SPLASH_DISPLAY_LENGTH = 3000;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
}
#Override
protected void onResume()
{
super.onResume();
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
// Obtain the sharedPreference, default to true if not available
boolean isSplashEnabled = sp.getBoolean("isSplashEnabled", true);
if (isSplashEnabled)
{
new Handler().postDelayed(new Runnable()
{
#Override
public void run()
{
//Finish the splash activity so it can't be returned to.
SplashScreenActivity.this.finish();
// Create an Intent that will start the main activity.
Intent mainIntent = new Intent(SplashScreenActivity.this, MainActivity.class);
SplashScreenActivity.this.startActivity(mainIntent);
}
}, 3000);
}
else
{
// if the splash is not enabled, then finish the activity immediately and go to main.
finish();
Intent mainIntent = new Intent(SplashScreenActivity.this, MainActivity.class);
SplashScreenActivity.this.startActivity(mainIntent);
}
}
}
If I change my manifest, and put the arround MainActivity, the application launches successfull
In my Run windows,I can see this message :
adb shell am start -n "me.project.com.project/me.project.com.project.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..
I had read in some similar question in SO that adding "android:exported="true" in in Manifest could resolve this problem, but it won't in my case.
Your wifi connectivity could be reason for this error.
It might be a good idea to reconnect to your wifi before making any code related changes.

Android KitKat not showing Toast once the App goes to background

I am running a service from my Android test app, which needs to show toast from counter value which is continuously increasing in a service.
Its starting well, and showing the toast. But, once I press back/home button to keep the app in background, the toast stops showing. When I again bring the app to foreground, again the toast started visible.
This problem happens sin Kitkat. But in JellyBeans and below, its working fine.
Here is my manifest file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rtrgroup.mysms"
android:installLocation="internalOnly">
<uses-sdk android:minSdkVersion="9"
android:targetSdkVersion="19"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true">
<activity
android:name="com.rtrgroup.mysms.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="com.rtrgroup.mysms.MyService"
android:enabled="true"
android:exported="true">
</service>
</application>
</manifest>
Here is my MainActivity.java file.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onStart() {
Intent serviceIntent = new Intent(this, MyService.class);
startService(serviceIntent);
}
}
Here is my service code, MyService.java:
package com.rtrgroup.mysms;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.*;
import android.widget.Toast;
public class MyService extends Service {
Handler handler;
int count = 0;
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
handler = new Handler();
handler.postDelayed(test, 1000);
return START_NOT_STICKY;
}
Runnable test = new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(), "TOAST count = " + String.valueOf(count), Toast.LENGTH_SHORT).show();
count++;
handler.postDelayed(test, 5000);
}
};
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
So, how to make toast visible always, even the app is in background and the service is running? Please explain.
if you want to detect when the user put your app in background , override this method
#Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);
switch (level) {
case ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN:
// do your magic here
break;
}
}
check the documentation http://developer.android.com/reference/android/content/ComponentCallbacks2.html#TRIM_MEMORY_UI_HIDDEN
also you can start your service in "foreground" mode for this check
Show notification from background started service
or you can show a notification instead a toast.

when service opens an activity, second instance of app creates

im working on app that require launch app from a service.
my problem is service create new instance of app Although my app is not closed. i want just one instance.
i find this question here like my problem but i can't use Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT
cause my apps crashes.
i change my tabactivity(main activity) to
android:launchMode="singleTop"
but problem still remains.
would some one guid me. thank you very much
this is my service:
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyAlarmService extends Service {
#Override
public void onCreate() {
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
openActivity(AlertActivity.class);
Log.i("LOG", "service started");
}
#Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
public void openActivity(Class activity) {
Intent i = new Intent(getBaseContext(), activity);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
}
and this is my manifest:
<application
android:icon="#drawable/drink1"
android:label="#string/app_name"
android:name=".G"
>
<activity
android:label="#string/app_name"
android:name=".MainTabActivity"
android:launchMode="singleTop" >
<!-- it cause to just one instance of app be exist -->
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AlertActivity" android:theme="#style/Theme.Transparent">
</activity>
<activity android:name=".DrinkoflifeActivity"
></activity>
<activity android:name=".SettingsActivity">
</activity>
<activity android:name=".StaticActivity">
</activity>
<activity android:name=".ContactUsActivity"></activity>
<activity android:name="org.achartengine.GraphicalActivity"/>
<activity android:name=".DataActivity">
</activity>
<activity android:name=".LogoActivity"></activity>
<activity android:name=".SplashActivity">
</activity>
<service android:name=".MyAlarmService" />
</application>
Setting launchMode="singleTop" on your MainActivity is probably not necessary and isn't going to help you here.
When your service starts AlertActivity, it will create a new instance of that activity. If your application has an active task, this will also result in the active task being brought to the foreground and the new instance of AlertActivity will be created on top of any other activities in the task. In a comment you wrote that AlertActivity leads to MainActivity then the problem is in how you are starting the MainActivity from the AlertActivity.
You should probably start the MainActivity from the AlertActivity this way:
Intent intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
Assuming that the MainActivity is the root activity in the task and there is still an active instance (ie: it has not yet been finished), this code will remove any other activities from the task stack and go back to the existing instance of MainActivity.

I am having issue with my splash screen as am not getting into the next activity

This is an example of splash screen but facing difficulty with it as I couldn't view my MainActivity class, couldn't be able to recognize the issue.Tried in manifest file by changing the action and the category name as well but could not be able to resolve to it.
Basically changed my Intent intent = new Intent(); as well but still the same goes on.
public class SplashActivity extends Activity
{
MediaPlayer player;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
player = MediaPlayer.create(SplashActivity.this, R.raw.splash);
player.start();
Thread timer = new Thread()
{
#Override
public void run()
{
try
{
sleep(4000);
}catch(InterruptedException e)
{
e.printStackTrace();
}
finish();
Intent intent = new Intent();
intent.setClass(SplashActivity.this, MainActivity.class);
startActivity(intent);
stop();
}
};
timer.start();
}
#Override
protected void onPause()
{
super.onPause();
player.release();
finish();
}
}
====>And here is my manifest file --
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mainsplashcreen"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/android_splash"
android:label="#string/app_name" >
<activity
android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".SplashActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I would guess, that your problem is, that you finish your activity before you start the new one. If you call finish() on your activity, it will be destroyed, and so the startActivity() won't be called anymore (or if it will be called, it won't get a valid context anymore). So try to move the finish() method, at the end of your run method, that should solve the problem.
Another Very Short,Simple and Effective way of Implementing Flash Screen is as below:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Handler handler=new Handler();
Runnable gotoMain=new Runnable() {
public void run()
{
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}
};
handler.postDelayed(gotoMain, 4000);
}
You just Replace you OnCreate Method with this one.
There are a few things which you should take care of:
1. The usage of stop() and the place it is being used are not encouraged. Besides being deprecated, it will also give you UnsupportedOperationExcecption.
2. Is your MediaPlayer being initialized correctly without any errors? Check the logs.
3. What is the reason you are using finish() in onPause() method? It is not recommended.
4. You are assigning MAIN action to both your activities. While it is allowed, it should be for a specific reason.
However, all these things should not avoid your application to go to the main activity. There may be the reasons with your main activity code.

Categories

Resources