I have a test live wallpaper that I wish to start from an activity. At the moment, it immediately finds itself in the live wallpaper chooser as I run the project on my emulator. Having searched everywhere, I still can't find a way to make it show up there only after a button in my main activity is pressed.
I tried introducing a static boolean in my main activity, isnotpressed, and setting it to false in the button onClick(). Then I used stopSelf() in my wallpaperservice class while isnotpressed is true. Unfortunately, none of this was to any avail and I don't think I'm going about it in the right way anyway. I also tried this, but it didn't work either. Any help would be deeply appreciated here.
Here's the main activity:
public class TestProjectActivity extends Activity{
static boolean isnotPressed;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
isnotPressed = true;
final Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
isnotPressed = false;
}
});
}
}
And here's part of the wallpaperservice:
public class MyWallpaperService extends WallpaperService {
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
while(TestProjectActivity.isnotPressed){stopSelf();}
}
Try this :
Intent i = new Intent();
if (Build.VERSION.SDK_INT > 15)
{
i.setAction(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
String pkg = MyPreferencesActivity.class.getPackage().getName();
String cls = MyPreferencesActivity.class.getCanonicalName();
i.putExtra(WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT, new ComponentName(pkg, cls));
}
else
{
i.setAction(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER);
}
startActivityForResult(i, 0);
Related
I want to open a class and a method of that class from another class simultaneously. When i click on button it stops the application. Help!
Method of class 'Fixtures' from which I want to call class and Method
public void onClick(View arg0) {
// TODO Auto-generated method stub
int id = arg0.getId();
FixtureDetails abc = new FixtureDetails();
abc.xyz(id);
startActivity(new Intent(Fixtures.this, FixtureDetails.class));
}
Class and method which I want to be opened
public class FixtureDetails extends Activity{
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fixturedetails);
tv = (TextView) findViewById(R.id.tv);
}
void xyz(int lmn)
{
switch(lmn)
{
case R.id.tvMatch1:
tv.setText("Hey there, wassup");
break;
}
}
}
Because Android handles the lifecycles of Activity classes it's not recommended to instantiate it directly, and calling that method like you are Android will recreate the class anyways destroying anything you changed in it.
The recommended way to do what you want is to use Intent Extras to pass data to your Activity.
public void onClick(View arg0) {
// TODO Auto-generated method stub
int id = arg0.getId();
Intent intent = new Intent(Fixtures.this, FixturesDetails.class);
intent.putExtra("id_key", id); // Set your ID as a Intent Extra
startActivity(intent);
}
FixtureDetails.class
public class FixtureDetails extends Activity{
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fixturedetails);
tv = (TextView) findViewById(R.id.tv);
Intent intent = getIntent();
if(intent != null && intent.getExtras() != null) {
xyz(intent.getIntExtra("id_key", -1)); // Run the method with the ID Value
// passed through the Intent Extra
}
}
void xyz(int lmn) {
switch(lmn) {
case R.id.tvMatch1:
tv.setText("Hey there, wassup");
break;
}
}
}
how can i retrieve the checkboxes value to update the database table on pressing the update button...
TeacherLoggedInClass
public class TeacherLoggedInPage extends Activity implements OnClickListener {
Button UpdateAttendanceButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.attendancesheet);
UpdateAttendanceButton = (Button) findViewById(R.id.bUpdateAttendance);
UpdateAttendanceButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
The isChecked() method return a boolean value with the information you need.
I would also suggest you use another way with implementing the Listener. This is what Google recommends:
public class MyActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click... for example your checkBox.isChecked()
}
});
}
If you want to get the single checkbox value and set database, you can follow below code,
public class TeacherLoggedInPage extends Activity implements OnClickListener {
Button UpdateAttendanceButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.attendancesheet);
UpdateAttendanceButton = (Button) findViewById(R.id.bUpdateAttendance);
UpdateAttendanceButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (cbBox.isChecked()) {
// do your operation
} else {
// do your operation
}
}
}
if you need to use a list of checkbox status to update databases, you'd better to create a collection to cache every checkbox status , and with bulkInsert or transaction to commit.
Hi!
I have a service that should be playing an audio file when it is started from an activity (by a click on the button). The problem is though, the service won't start when i click the required button in my activity.
Service class (with imports and some other methods omitted):
public class ClockService extends Service {
MediaPlayer myMediaPlayer;
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
myMediaPlayer = MediaPlayer.create(this, R.raw.audio);
myMediaPlayer.setLooping(true);
}
#Override
public void onStart(Intent intent, int startid) {
myMediaPlayer.start();
}
}
The activity it is called from:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startButton = (Button) findViewById(R.id.startButton);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startService(new Intent(getBaseContext(), ClockService.class));
}
});
}
}
I've struggled with it for an hour already, and i can't see what's wrong.
u got problem on below code line.u cant use this for context inside service.u need to pass context of mainactivity to service to get mediaplayer instance inside service.
myMediaPlayer = MediaPlayer.create(this, R.raw.audio);//wrong one
The problem is how you started the service
Change this line to startservice(new intent(mainactivity.this, clockservice));
here is a link you can refer to.
I have an Android App That requires a button to open up a new xml page. This is what it's like now, could someone add the necessary code to make it open Page2Activity when I click on the button? Code:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Void onClick;View arg0; {
// TODO Auto-generated method stub
}
};
}
I figured this out using this method : http://stackoverflow.com/questions/4094103/linking-xml-pages-with-layout ,but I will try All of yours as well.
Try this code:
public void handleClick(View v){
//Create an intent to start the new activity.
Intent intent = new Intent();
intent.setClass(this,Page2Activity.class);
startActivity(intent);
}
Then create a new cLass called Page2Activity.
Hope this helps and don't forget to add your activity to the manifest file.
i think you mean something like this:
public class MyClass extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent("com.myaction");
startActivity(i);
}
});
}
}
I'm trying to make a simple notepad application and I would like to refresh the notes when the New Note activity finishes and the main screen resumes. However I get force close when trying to open the application with this code. If I remove the OnResume thing it doesn't force close. Help?
public class NotePadActivity extends Activity implements View.OnClickListener {
TextView tw;
String data;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tw = (TextView)findViewById(R.id.uusi);
tw.setOnClickListener(this);
Note note = new Note(this);
note.open();
data = note.getData();
note.close();
tw.setText(data);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.uusi:
try {
startActivity(new Intent(PadsterActivity.this, Class.forName("com.test.notepad.NewNote")));
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Note note = new Note(this);
note.open();
data = note.getData();
note.close();
tw.setText(data);
}
}
The problem is you have two different TextViews called tw see my comments on your code...
public class NotePadActivity extends Activity implements View.OnClickListener {
TextView tw; // This never gets instantiated
...
Another here...
public void onCreate(Bundle savedInstanceState) {
...
// This is instantiated but is local to onCreate(...)
TextView tw = (TextView)findViewById(R.id.uusi);
Then in onResume(...) you attempt to use the instance member tw which is null...
protected void onResume() {
...
tw.setText(data);
Change the line in onCreate to...
tw = (TextView)findViewById(R.id.uusi);
...and it should fix the problem.
BTW, you don't need to duplicate everything in onCreate(...) again in onResume() as onResume() is always called after onCreate(...) when an Activity is created.