What I'm trying to do is Live Wallpaper with some action(that is already done)but the problem is there..that I've made it in Activity which "action" is an class who extends view and the Activity's setContentView() method isn't some layout ..it is this View.And what I'm trying to do now is to create option to set that activity as wallpaper,but I don't know how. This is my onCreate() method from the main Activity which is loading the View. I want that to be an Wallpaper..so how can I set it up to be one?
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SnowFall snowFallView = new SnowFall(this);
setContentView(snowFallView);
snowFallView.setBackgroundDrawable(getResources().getDrawable(
R.drawable.christmas));
}
Here's example app for what I want to create: https://play.google.com/store/apps/details?id=com.chiralcode.wallpaper.autumn&hl=bg
Did you implement wallpaper as a service? Get the basics - check Lars Vogel's tutorial on live wallpapers here
In short, you can set the wallpaper with an intent:
Intent intent = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
intent.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, new ComponentName(this, MyWallpaperService.class));
startActivity(intent);
You will need to adapt the above to your class names, and remember to have the permission android.permission.BIND_WALLPAPER in your AndroidManifest, but again, read his tutorial.
Start Wallpaper service through activity , following is perfect working for that, you can put following in onclick(...) also, if you start livewallpaper ( your own live wallpaper directly through just one click) you just write following code,
btnInstallWallpaper.setOnClickListener(new OnClickListener() {
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
#SuppressLint("InlinedApi")
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
if (android.os.Build.VERSION.SDK_INT >= 16)
{
intent.setAction("android.service.wallpaper.CHANGE_LIVE_WALLPAPER");
intent.putExtra("android.service.wallpaper.extra.LIVE_WALLPAPER_COMPONENT", new ComponentName("com.example.wall", "com.example.wall.WallpaperService"));
} else
intent.setAction("android.service.wallpaper.LIVE_WALLPAPER_CHOOSER");
intent.putExtra("android.service.wallpaper.extra.LIVE_WALLPAPER_COMPONENT", new ComponentName("com.example.wall", "com.example.wall.WallpaperService")); // package + classname
//}
startActivity(intent);
}
});
Related
I'm a newbie and I'm working on a Unit Converter.
I would like to open the same UI regardless of which button I click(Weight or Length)
main_activity.xml UI
Below is the UI I want to open: activity_conversion.xml UI
And I would like each button in the main_activity to run on a different java class.
So, (minus the main_activity.java and it's .xml file)
I have 1 xml file(activity_conversion.xml) and 2 java files one for each button of the main_activity.xml
activity_main.xml Weight button
android:onClick="weightPage"
activity_main.xml Length button
android:onClick="lengthPage"
MainActivity.java
public void weightPage(View view){
Intent intent = new Intent(this, WeightActivity.class);
startActivity(intent);
}
public void lengthPage(View view) {
Intent intent2 = new Intent(this, LengthActivity.class);
startActivity(intent2);
}
Length_Activity.java code for Length button
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_conversion);
}
setContentView() method doesn't work for me:(
Thanks in advance!
I would like to open the same UI regardless of which button I click(Weight or Length)
You can do that by creating an activity and its layout XML file. And then start that activity via explicit intent like this:
//Place this code inside the onClick method
Intent intent = new Intent(SoucreActivity.this, DestinationActivity.class);
startActivity(intent);
And I would like each button in the main_activity to run on a different java class.
No, you cannot. All UI elements on a screen are always in the same activity; they cannot run on different java classes. (Unless you are using fragments of which you need not worry about as you are a newbie)
Apparently, you want the two buttons in your main activity to open the same activity. Which you can achieve using intents using the code snippet mentioned above.
In an activity, I have created a AsyncTask after hiding the activity:
this.moveTaskToBack(true);
(new MyTask(this)).execute();
To show a dialog in the task (in onPostExcecute), I want to bring the activity to front:
alertDialog.show();
Intent intent = new Intent(mainActivity, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mainActivity.getBaseContext().startActivity(intent);
But a new instance of the main activity is created and shown on top of the dialog, although the application was still running (the activity has also a dialog style Theme.Dialog). How should I fix this?
Edit: According to javadoc, this code always recreates the activity and doesn't bring its previous instance to front, since startActivity is called from outside of an Activity Context.
How about adding a new piece of information to that intent, and catching it in onCreate()?
What I mean is something like this:
public class MainActivity extends Activity {
public static final String WANT_DIALOG_EXTRA = "WANT_DIALOG_EXTRA";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getIntent().hasExtra(WANT_DIALOG_EXTRA)) {
// create and show dialog
}
}
}
Then when you create your intent, add one more line like this:
intent.putExtra(MainActivity.WANT_DIALOG_EXTRA, true);
I have three activities: First, Second and Third. I used this method in Second activity:
public void onBackPressed() {
super.onBackPressed();
finish();
}
and this on Third activity:
public void onBackPressed() {
super.onBackPressed();
Intent i = new Intent(Third.this, Second.class);
startActivity(i);
finish();
}
The problem is when I press back button after coming from the Third activity, I am going into First activity instead of finish(). I am successfully exiting the application when I click back button right after coming from first activity but not after coming from Third activity.
How to solve this problem?
EDIT: Thanks for the answers guys,the answer of "Ved Prakash" solved the problem for me.But i have a weird problem now.When i press back button the app is successfully exiting but the app which i minimized to Recent Apps button is coming on to the screen and exiting.For example,if i have opened Setting app before opening my app,when i press back button,my app is exiting and immediately Settings app is also opening and exiting itself.What might be the problem?
Your problem is that you don't seem to understand how Activities work. The finish() function ends the current Activity, and then you receive the previous Activity from the backstack.
My recommendation is that you should use a single Activity, and hold Fragments inside it. If you want it so that pressing the Back button ends the application at any screen that is displayed, you could do the following:
Activity XML:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/initial_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Activity that holds the Fragments:
public class InitialActivity extends FragmentActivity implements ReplaceWith
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_initial);
getSupportFragmentManager().addOnBackStackChangedListener(new OnBackStackChangedListener()
{
public void onBackStackChanged()
{
int backCount = getSupportFragmentManager().getBackStackEntryCount();
if (backCount == 0)
{
finish();
}
}
});
if (savedInstanceState == null)
{
getSupportFragmentManager().beginTransaction().add(R.id.initial_container, new FirstFragment()).commit();
}
}
#Override
public void replaceWith(Fragment fragment)
{
getSupportFragmentManager().beginTransaction().replace(R.id.initial_container, fragment).commit();
}
}
Example for a Fragment:
public class FirstFragment extends Fragment implements View.OnClickListener
{
private ReplaceWith activity_replaceWith;
private ImageView exampleImage;
public FirstFragment()
{
super();
}
#Override
public void onAttach(Activity activity)
{
super.onAttach(activity);
try
{
activity_replaceWith = (ReplaceWith) activity;
}
catch (ClassCastException e)
{
Log.e(getClass().getSimpleName(), "Activity of " + getClass().getSimpleName() + "must implement ReplaceWith interface!", e);
throw e;
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_first, container, false);
exampleImage = (ImageView) rootView.findViewById(R.id.fragment_first_example_image);
exampleImage.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v)
{
if(v == exampleImage)
{
activity_replaceWith.replaceWith(new SecondFragment());
//please note that this should be done only if you are planning
//only on single-screen applications
//with no other layouts based on orientation or size
//otherwise, the Activity needs to be responsible for this, not the Fragment
}
}
}
This way, when you press the Back button, your application would end from any displayed screen.
Ok your code is wrong.
If you will look at activity source, you see that activity.onBackPressed() is calling finish(). So if call super.onBackPressed() you don't need to call finish.
Finish() is not stopping your application, it's stopping current activity.
Your code on third activity very strange. You are trying to stop activity and start another same activity.
What exactly you want to achieve?
If you want to exit application from your third activity, you need to clear your backstack. But I think you have problem with structure of your app.
Ok. then you should finish your first activity when you go to second activity like this(If you are using intent for that):
Intent it=new Intent(FirstActivity.this,SecondActivity.class);
finish();
startactivity(it);
and same for Second Activity:
Intent it=new Intent(SecondActivity.this,ThirdActivity.class);
finish();
startactivity(it);
this done your work...when you are in third activity the above activities are finished..
and when you press backButton you will be exit from application..
Good luck.
You can use -
public static final int FLAG_ACTIVITY_CLEAR_TOP
If set, and the activity being launched is already running in the
current task, then instead of launching a new instance of that
activity, all of the other activities on top of it will be closed and
this Intent will be delivered to the (now on top) old activity as a
new Intent.
And here is how -
When the user wishes to exit all open activities, they should press a button which loads the first Activity that runs when your app starts, in my case "MainActivity".
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
The above code clears all the activities except for LoginActivity. LoginActivity is the first activity that is brought up when the user runs the program. Then put this code inside the LoginActivity's onCreate, to signal when it should self destruct when the 'Exit' message is passed.
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
This explanation part is also introduced at exit-an-android-app.
in my app the main page contains, quite a few images to load on my upload manager activity so it can take a few seconds, depending on how many images there are. i planned on creating a splashscreen to do this loading while displaying an image which is not as bad as the default blank screen with title. i have done this, which should work and does, except the setcontentview() does run but does not display.
public class SplashScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
load l=new load();
l.execute(this);
}
class load extends AsyncTask<Activity, Object, Object>{
#Override
protected Object doInBackground(Activity... a) {
// TODO Auto-generated method stub
Log.i("ss", "splash");
Intent intent = new Intent(a[0], UploadManager.class);
startActivity(intent);
a[0].finish();
return null;
}
}
}
does anybody have any suggestions?
and feel free to ask for details i don't think i have explained it all too well.
edit:
thank you guys for the quick responses.
however i believe the problem was that i wasn't using a splash screen for the correct purpose,
the processes involved in:
Intent intent = new Intent(a[0], UploadManager.class);
startActivity(intent);
a[0].finish();
seem to finish instantly, meaning the images in my onCreate method weren't executing until after the splash screen. what i did instead is changed the loading of my grid into an asynktask, as apposed to just doing my images in there.
i now have it loading fast with the images appearing after a few seconds. i shall be implementing a progress dialog of some sort.
anyone else with a similar problem should prioritize making the loading more efficient as i have.
You are passing Context as this in l.execute(this) and in class load you've passed Activity instance.
You can do it in this way and it works like a charm for me
public class SplashScreen extends Activity{
private static int SLPASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
finish();
}
}, SLPASH_TIME_OUT);
}
}
Override onPostExecute method in class load extends AsyncTask<Activity, Object, Object>{ class, which will run when your doInBackground method finishs image downloading.
In onPostExecute you can open your next activity
like
protected void onPostExecute(Void unused) {
Intent intent= new Intent(this, next.class);
startActivity(intent);
}
I'm trying to define a custom Java class (extending a LinearLayout), which needs to start an activity on click. My code looks like this :
public ArizaSatiri(Context context/*, AttributeSet attrs , final Activity aktivite*/ , JSONObject mysql_satiri)
{
super(context/*, attrs*/);
// code to initialize my view :
final Context finalContext = context;
this.setOnClickListener(new OnClickListener() {#Override
public void onClick(View v) {
Intent newIntent = new Intent(finalContext, ArizaDetaylari.class);
finalContext.startActivity(newIntent);
}//onClick
});
}
But when I clicked on the instantiated view, I get the error :
Calling startActivity() from outside of an Activity context requires the
FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
As you can see from the code, I tried passing the Activity to the constructor, and that worked. But is that the correct way? Which way would you reccomend ?
Edit:
And I also need to call setTypeFace() at some point. Should I use context, or Activity for that ?
Try this:
this.setOnClickListener(new OnClickListener() {#Override
public void onClick(View v) {
Intent newIntent = new Intent(finalContext, ArizaDetaylari.class);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
finalContext.startActivity(newIntent);
}//onClick
});
You can put your code into onAttachedToWindow() of your custom view class.
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
final Context context = ArizaSatiri.this.getContext();
Intent intent = new Intent(context , ArizaDetaylari.class);
context.startActivity(intent);
}
});
}
This helps because this function is called after your view is added to the activity, the view has the reference of it. Constructor of views probably run before being added to the activity, thus the error.
This should also work if you declare your view in xml instead of creating it programmatically. (Not tested yet)
If take a look to the Context class reference you could see that Activity is an indirect subclass. So if you use an Activity as Context your code should work.
For example:
public ArizaSatiri(Context context, JSONObject mysql_satiri){ ...}
you can change the call to the ArizaSatiri constructor:
new ArizaSatiri(myActivity, mysql_satiri);
Hope it helps.
If context (finalContext) that you are using is referring to ApplicationContext then you need to use the flag. Just add the sentence newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); before finalContext.startActivity(newIntent);
Problem with this approach especially when you are starting something outside of your app for ex: mail client etc, is it will continue to be there in the recent apps stack even after the activity is completed, in case of mail client it continues to be there even after mail has been sent