Lock App - Java for Android App Development

i am trying to make a lockscreen replacement app. I want my app to diplay instead of android lockscreen .
I 've done some search and made it work fine , but when home button is pressed my app exits. I've read that it is not possible to ignore home button on android >4.0.1
But apps on playstore don't have this problem. There must be a way not to exit on home button pressed

TakisBeskos said:
i am trying to make a lockscreen replacement app. I want my app to diplay instead of android lockscreen .
I 've done some search and made it work fine , but when home button is pressed my app exits. I've read that it is not possible to ignore home button on android >4.0.1
But apps on playstore don't have this problem. There must be a way not to exit on home button pressed
Click to expand...
Click to collapse
Try this I am forget after it
@override
public void onPause() {
super.onPause();
return;
}
@override
public void onBackPressed() {
super.onBackPressed();
return;
}
@override
public void onStop() {
super.onStop();
return;
}
@override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return keyCode == KeyEvent.KEYCODE_HOME
|| keyCode == KeyEvent.KEYCODE_BACK;
}
Sent from my SM-G530H using XDA Free mobile app

AndroidFire said:
Try this I am forget after it
@override
public void onPause() {
super.onPause();
return;
}
@override
public void onBackPressed() {
super.onBackPressed();
return;
}
@override
public void onStop() {
super.onStop();
return;
}
@override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return keyCode == KeyEvent.KEYCODE_HOME
|| keyCode == KeyEvent.KEYCODE_BACK;
}
Sent from my SM-G530H using XDA Free mobile app
Click to expand...
Click to collapse
it didn't work Any other ideas ?

AndroidFire said:
Try this I am forget after it
@override
public void onPause() {
super.onPause();
return;
}
@override
public void onBackPressed() {
super.onBackPressed();
return;
}
@override
public void onStop() {
super.onStop();
return;
}
@override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return keyCode == KeyEvent.KEYCODE_HOME
|| keyCode == KeyEvent.KEYCODE_BACK;
}
Sent from my SM-G530H using XDA Free mobile app
Click to expand...
Click to collapse
You don't need return statements on void methods. By definition void doesn't return anything - your IDE should have already notified you of that.
Edit: Do you even know what the code you wrote does? Why on earth are you overriding all those super methods then just calling the super method from within the overridden method - your code makes absolutely no sense whatsoever.

i am searching a few weeks and i didn't find a thing.
The only idea i have is to make my lockscreen app a launcher , so home button will not exit the locksceen, and when user unlocks it , to open an installed launcher.
but how can i get installed launchers ?

Jonny said:
You don't need return statements on void methods. By definition void doesn't return anything - your IDE should have already notified you of that.
Edit: Do you even know what the code you wrote does? Why on earth are you overriding all those super methods then just calling the super method from within the overridden method - your code makes absolutely no sense whatsoever.
Click to expand...
Click to collapse
can't reconsize it through phone
Sent from my SM-G530H using XDA Free mobile app

TakisBeskos said:
i am searching a few weeks and i didn't find a thing.
The only idea i have is to make my lockscreen app a launcher , so home button will not exit the locksceen, and when user unlocks it , to open an installed launcher.
but how can i get installed launchers ?
Click to expand...
Click to collapse
Try looking here for disabling home button - I'm not sure if it works but might be worth a try:
http://stackoverflow.com/questions/21983462/creating-custom-lockscreen-in-android/28603790#28603790
As for getting a list of installed launchers try the below link:
http://stackoverflow.com/questions/6175821/get-a-list-of-every-launcher-in-android

Jonny said:
Try looking here for disabling home button - I'm not sure if it works but might be worth a try:
http://stackoverflow.com/questions/21983462/creating-custom-lockscreen-in-android/28603790#28603790
As for getting a list of installed launchers try the below link:
http://stackoverflow.com/questions/6175821/get-a-list-of-every-launcher-in-android
Click to expand...
Click to collapse
nope. it didn't work

Related

[Q]Calling a method from another Class

I had a class which i wanted to split into two for better readability and whenever i want to start a method from the second class the code gives NullPointerException.
So i ended up making another dummyclass and realized the problem is that either i dont know to to call from another class in android or am facing another issue.
In java id just make an object and call it but here NullPointerException and app crashes
Code:
public class active extends Activity {
public Button but;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void click()
{
but = (Button)findViewById(R.id.wifiSwitch);
but.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Rebooting...", Toast.LENGTH_LONG).show();
}
});
}
}
In the first class i do the following
Code:
[user=439709]@override[/user]
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
rootReqSetting();
mActive = new active();
mActive.click();
return true;
}
the mActive = new active(); and mActive.click(); causes the nullexception.
Some help please ?
Well, first I request that you post logcats in the future.
Second: You call that:
Code:
but = (Button)findViewById(R.id.wifiSwitch);
but.setOnClickListener(new View.OnClickListener() {
What is the system doing? There is a new object whose onCreate method has never been called (just the constructor). For that reason the field but is null (equals "no value"). That's why it crashes.
Third: You do not do it that way:
mActive = new active();
Starting other Activities is done using Intents. Read this: http://www.vogella.com/articles/AndroidIntent/article.html
Where in the code did you inflate the wifi button ????
That is the cause of NullPointerException
Secondly,
You cant instantiate activities like this you must use
startActivity(Intent);
Sent from my GT-S5302 using Tapatalk 2
Thank you for you answer.
The Third part i understood that i should make an intent for the second class.
About the second part you said i didnt understand correctly , can you explain a little more ?
 @sak-venom1997 what do you mean by inflate the button, didnt the whole menu get inflated when i called it in the first ?
Code:
getMenuInflater().inflate(R.menu.main, menu);
or there is something i should add?
And a new window will show when i start the intent without calling anything in it. i just want the button to do what it is supposed to do.
Tell me how the hell did you manage to put a Button in a MenuItem that code would just infate the menu Button would be infalted only if the parent layout is inflated
Sent from my GT-S5302 using Tapatalk 2
sak-venom1997 said:
Tell me how the hell did you manage to put a Button in a MenuItem that code would just infate the menu Button would be infalted only if the parent layout is inflated
Sent from my GT-S5302 using Tapatalk 2
Click to expand...
Click to collapse
can you tell me then what am supposed to do, Am new to android and trying to learn.
You must call
setContentView(R.layout.layout_name);
Where is that button ure reffering
Sent from my GT-S5302 using Tapatalk 2

Need some help with preference manager please.

Alright guys, first app here. I hit a bump in the road, can someone tell me how to use preference manager to save and restore my switch states?
A snippet of my code,
Code:
public class MainActivity extends Activity implements View.OnClickListener {
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PreferenceManager.getDefaultSharedPreferences(this).getBoolean("sw1",false);
}
public void onClick(View v) {}
public void sw1(View view) {
// Is the view now checked?
boolean checked = ((Switch) view).isChecked();
// Check which checkbox was clicked
switch(view.getId()) {
case R.id.sw1:
if (checked) try {
//Toast.makeText(this, "SW1 Checked", Toast.LENGTH_SHORT).show();
Runtime.getRuntime().exec(new String[] { "su","-c","mod1" });
PreferenceManager .getDefaultSharedPreferences(this).edit().putBoolean("sw1",true).commit();
} catch (IOException e) {
e.printStackTrace();
}
else try {
//Toast.makeText(this, "SW1 NOT Checked", Toast.LENGTH_SHORT).show();
Runtime.getRuntime().exec(new String[] { "su","-c","mod1b" });
PreferenceManager .getDefaultSharedPreferences(this).edit().putBoolean("sw1",false).commit();
} catch (IOException e) {
e.printStackTrace();
break;}
}
}
tmlhodge said:
Alright guys, first app here. I hit a bump in the road, can someone tell me how to use preference manager to save and restore my switch states?
Click to expand...
Click to collapse
Try this:
Code:
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean checked = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("sw1",false);
Switch mSwitch = (Switch )findViewById(R.id.sw1);
mSwitch.setChecked(checked);
}
I'm not sure where you setup your click/check listeners.. If you could provide some more details, that would be great.
Hope this helps!
alobo said:
Try this:
Code:
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean checked = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("sw1",false);
Switch mSwitch = (Switch )findViewById(R.id.sw1);
mSwitch.setChecked(checked);
}
I'm not sure where you setup your click/check listeners.. If you could provide some more details, that would be great.
Hope this helps!
Click to expand...
Click to collapse
Alright, the on click listener is right before the list of buttons, you can actually see it in the code in the OP.
Also I am starting to get excited because that actually made my switch turn on! But now it's just on every time I turn it off and exit app..
Sent from my HTC One using Tapatalk HD
That got me on the right track and I figured the rest out! Thank you very much good sir/mam @alobo

[Guide] Listeners in Java development

You are new to java development and want to get buttons working?
Maybe you are a Pro but want a reminder?
whatever you are this Guide is to help you to make buttons/check boxes...etc working and functional
Some people are distracted between guides over internet and want the easiest way to get their project working, me too
Steps :
1-Define the button :
Code:
Button btn1;
Checkbox chkbox1;
RadioButton radio1;
2- Intialize it :
Code:
btn1 = (Button) findViewById(R.id.btn1);
chkbox1= (Checkbox ) findViewById(R.id.chkbox1);
radio1= (RadioButton) findViewById(R.id.radio1);
3-Add the listener :
Button:
Code:
btn1.setOnClickListener(new OnClickListener() {
@SuppressWarnings("deprecation")
@Override
public void onClick(View arg0) {
//Write awesome code here
}
});
CheckBox :
Code:
chkbox1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (start.isChecked()) {
//if the checkbox checked
} else {
//if not checked
}
}
});
}
radio button:
Code:
public void onRadioButtonClicked(View view) {
boolean checked = ((RadioButton) view).isChecked();
switch(view.getId()) {
case R.id.radio1:
if (checked){
}
else{
}
break;
}
}
or use it in a radio Group :
Code:
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio1:
if (checked)
//Write code
break;
case R.id.radio2:
if (checked)
//Write code
break;
}
}
Also insted of this you can use a onCheckedChanged for a radio button (Thanks for GalaxyInABox)
Code:
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (radioGroup.getCheckedRadioButtonId()) {
//Code
}
}
____________________________________________________________________
____________________________________________________________________
Also you can implement a Onclicklistener for the whole class to save resources : (thanks for @Jonny )
after defining and initializing your objects add this :
Code:
OnClickListener click_listener = new OnClickListener() {
public void onClick(View view) {
int id = view.getId();
if (id == your_id) {
//do stuff for this object
} else if (id == your_id2) {
//do other stuff for diffrent object
} else if (id == your_id3) {
//and so on
}
}
};
To do list :
-add on touch listeners
-add on drag listeners
Note : you can add a click listener to almost any thing (Textview or imageView or even EditText) just using the same method of adding listener to button
also there is some other ways to add a listener but this is the fastest and less disturbing :good:
If this guide is useful, press thanks
@ OP
CheckBox and RadioButtons don't they provide a CheckedChangeListener ?
Sent from my GT-S5302 using Tapatalk 2
sak-venom1997 said:
@ OP
CheckBox and RadioButtons don't they provide a CheckedChangeListener ?
Click to expand...
Click to collapse
Yes, and now you can use
Code:
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (radioGroup.getCheckedRadioButtonId()) {
//Code
}
}
to get the checked button. They are pretty much the same, but you can use view.getTag() easier in the first one.
And @mohamedrashad please show how to put the listener into a inner class. Many people don't know/use it, but it's that useful!
GalaxyInABox said:
Yes, and now you can use
Code:
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (radioGroup.getCheckedRadioButtonId()) {
//Code
}
}
to get the checked button. They are pretty much the same, but you can use view.getTag() easier in the first one.
Click to expand...
Click to collapse
I meant that the op shuld edit this guide and use those instead of OnCickListeners
GalaxyInABox said:
And @mohamedrashad please show how to put the listener into a inner class. Many people don't know/use it, but it's that useful!
Click to expand...
Click to collapse
ya new with java8 it will be a nice usage scenario of lambadas
Sent from my GT-S5302 using Tapatalk 2
GalaxyInABox said:
Yes, and now you can use
Code:
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (radioGroup.getCheckedRadioButtonId()) {
//Code
}
}
to get the checked button. They are pretty much the same, but you can use view.getTag() easier in the first one.
And @mohamedrashad please show how to put the listener into a inner class. Many people don't know/use it, but it's that useful!
Click to expand...
Click to collapse
sak-venom1997 said:
@ OP
CheckBox and RadioButtons don't they provide a CheckedChangeListener ?
Sent from my GT-S5302 using Tapatalk 2
Click to expand...
Click to collapse
ok, i will add this
You can also add onClick property in XML and then handle it in a code.
Awesome tutorial! Thank you very much!
Please, you could share more related knowledge. It's really useful!
Also, an activity can be a listener. In this case:
MyActivity implements onClickListener {
btn1.setOnClickListener(this);
public void onClick (View v) {
//your code
}
}
For this kind of stuff, using some well known libraries from well known Android dev is a must.
https://github.com/JakeWharton/butterknife
Very powerfull, super easy to use, error prone and without any performance impact.
rafalniski said:
You can also add onClick property in XML and then handle it in a code.
Click to expand...
Click to collapse
SKAm69 said:
Also, an activity can be a listener. In this case:
MyActivity implements onClickListener {
btn1.setOnClickListener(this);
public void onClick (View v) {
//your code
}
}
Click to expand...
Click to collapse
will add them both, although I don't like this way
Mohamedrashad. Thanks a lot.
Sent from my P880 using Tapatalk
If you have multiple clickable objects then it's best to use just 1 onClickListener for all of them and use a switch on their ID's. This reduces resource usage as you only have 1 listener, not 5, 10 or however many you would have otherwise. It's not essential for this but it is a best practice if you want to streamline your code.
Mobile right now so I can't chuck up an example until tomorrow evening or so.
You dude had a great thread. Its helping me. Bravoo !!
Sent from my GT-I8190 using XDA Premium 4 mobile app
As @Jonny already pointed out: Use your class as a listener instead of creating a new (anonymous) inner class! Say you have a ListView, instead of doing this:
Code:
class MyFragment extends Fragment {
private void someMethod() {
((ListView) getView().findViewById(R.id.someListView)).setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Code...
}
});
}
}
you can do this:
Code:
class MyFragment extends ListFragment implements AdapterView.OnItemClickListener, View.OnClickListener {
private void someMethod() {
((ListView) getView().findViewById(R.id.someListView)).setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Code...
}
}
This may look stupid, but when you have many listeners, you can un-clutter it. In my opinion this is the best way. You can also add "this" class as listener for as many ui elements as you want(because all of them extend view, you can use one OnClickListener), then you only need to have a switch statement to distinguish between the views. And voila, you prevented cluttering the code with boilerplate stuff.
Example using code in an app I'm making - app for my school.
Code:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Some code here for view/layouts etc
websitebutton = (Button) view.findViewById(R.id.website_btn);
facebookbutton = (Button) view.findViewById(R.id.facebook_btn);
twitterbutton = (Button) view.findViewById(R.id.twitter_btn);
websitebutton.setOnClickListener(handler);
facebookbutton.setOnClickListener(handler);
twitterbutton.setOnClickListener(handler);
return view;
}
OnClickListener handler = new OnClickListener() {
public void onClick(View view) {
switch (view.getId()) {
case R.id.website_btn :
Uri website = Uri.parse("http://wirralgrammarboys.com/");
Intent websiteintent = new Intent(Intent.ACTION_VIEW, website);
startActivity(websiteintent);
break;
case R.id.facebook_btn :
Uri facebook = Uri.parse("https://www.facebook.com/WirralGSB");
Intent facebookintent = new Intent(Intent.ACTION_VIEW, facebook);
startActivity(facebookintent);
break;
case R.id.twitter_btn :
Uri twitter = Uri.parse("https://twitter.com/WGSB");
Intent twitterintent = new Intent(Intent.ACTION_VIEW, twitter);
startActivity(twitterintent);
break;
}
}
};
Jonny said:
Example using code in an app I'm making.
Code:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Some code here for view/layouts etc
websitebutton = (Button) view.findViewById(R.id.website_btn);
facebookbutton = (Button) view.findViewById(R.id.facebook_btn);
twitterbutton = (Button) view.findViewById(R.id.twitter_btn);
websitebutton.setOnClickListener(handler);
facebookbutton.setOnClickListener(handler);
twitterbutton.setOnClickListener(handler);
return view;
}
OnClickListener handler = new OnClickListener() {
public void onClick(View view) {
int id = view.getId();
if (id == R.id.website_btn) {
Uri website = Uri.parse("http://wirralgrammarboys.com/");
Intent websiteintent = new Intent(Intent.ACTION_VIEW, website);
startActivity(websiteintent);
} else if (id == R.id.facebook_btn) {
Uri facebook = Uri.parse("https://www.facebook.com/WirralGSB");
Intent facebookintent = new Intent(Intent.ACTION_VIEW, facebook);
startActivity(facebookintent);
} else if (id == R.id.twitter_btn) {
Uri twitter = Uri.parse("https://twitter.com/WGSB");
Intent twitterintent = new Intent(Intent.ACTION_VIEW, twitter);
startActivity(twitterintent);
}
}
};
Click to expand...
Click to collapse
i'm adding this to OP if you don't mind jonny
mohamedrashad said:
i'm adding this to OP if you don't mind jonny
Click to expand...
Click to collapse
That's fine - if I didn't want people to use/adapt/learn from the code then I wouldn't put it up, use it as you want :good:
Sent from my HTC One using Tapatalk
Keep it up
Great tutorials, keep em coming!
Hey what about starting a new activity with onClickListiner
Sent from my M3S_D7 using XDA Free mobile app
---------- Post added at 03:57 PM ---------- Previous post was at 03:49 PM ----------
Hey and do u mind sending a source codes.zip file
Sent from my M3S_D7 using XDA Free mobile app
Rebound.co said:
Hey what about starting a new activity with onClickListiner
Sent from my M3S_D7 using XDA Free mobile app
---------- Post added at 03:57 PM ---------- Previous post was at 03:49 PM ----------
Hey and do u mind sending a source codes.zip file
Sent from my M3S_D7 using XDA Free mobile app
Click to expand...
Click to collapse
in the onClick method just have this code:
Code:
startActivity(new Intent(this, YourActivity.class));

[Q]Service doesn't stop even after calling stopService

Here is the service
Code:
public class SearchService extends IntentService {
public SearchService() {
super("SearchService");
// TODO Auto-generated constructor stub
}
// Binder given to clients
/**
* Class used for the client Binder. Because we know this service always
* runs in the same process as its clients, we don't need to deal with IPC.
*/
[user=439709]@override[/user]
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
String FILENAME=intent.getStringExtra("name");
String FILEPATH=intent.getStringExtra("path");
ArrayList a=getSearchResult(new File(FILEPATH),FILENAME);
Toast.makeText(this, "Search Completed", Toast.LENGTH_LONG).show();
publishResults(a);
this.stopSelf();
}
private void publishResults(ArrayList<File> outputPath) {
Intent intent = new Intent("notify");
ArrayList<String> a=new ArrayList<String>();
for(int i=0;i<outputPath.size();i++){a.add(outputPath.get(i).getPath());}
intent.putStringArrayListExtra("path", a);
sendBroadcast(intent);
} private void publishResults(String a) {
Intent intent = new Intent("current");
intent.putExtra("name", a);
sendBroadcast(intent);
}}
I am using it like this
Code:
final Intent intent = new Intent(getActivity(), SearchService.class);
intent.putExtra("path",fpath);
intent.putExtra("name",a);
p=new ProgressDialog(getActivity());
p.setCancelable(false);
p.setTitle("Searching Files");
p.setMessage("Please Wait");
p.getWindow().addFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON );
p.setButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface p1, int p2)
{
getActivity().stopService(new Intent(getActivity(),SearchService.class));
// TODO: Implement this method
}
});
p.show();
getActivity().startService(intent);
but even after pressing cancel button,broadcast is received in activity
Sent from my GT-S5570 using XDA Premium 4 mobile app
A service to display a toast and brodcast the data it recieved looks like a design flaw
anyways you are extending the intent service i guess it does not implement stopService() rather it stops automatically when it has nothing to do[not sure with it please check documentation for IntentService never actually used one of those ]
I guess you need to extend the Service class from package android.app
Sent from my GT-S5302 using Tapatalk 2
sak-venom1997 said:
A service to display a toast and brodcast the data it recieved looks like a design flaw
anyways you are extending the intent service i guess it does not implement stopService() rather it stops automatically when it has nothing to do[not sure with it please check documentation for IntentService never actually used one of those ]
I guess you need to extend the Service class from package android.app
Sent from my GT-S5302 using Tapatalk 2
Click to expand...
Click to collapse
Actually I am using toast just for debugging.I am learning services. so I might be wrong at places.I made this service to search for files while an indeterminate progress dialog shows in activity till the broadcast of result is received.
I used intentservice because it was supposed to do one work at a time.please suggest me exact ways to use service in my case.I also want to make sure that if activity is paused(minimized) then, when task is completed activity is also started
Sent from my GT-S5570 using XDA Premium 4 mobile app
arpitkh96 said:
Actually I am using toast just for debugging.I am learning services. so I might be wrong at places. I made this service to search for files while an indeterminate progress dialog shows in activity till the broadcast of result is received.
I used intentservice because it was supposed to do one work at a time.please suggest me exact ways to use service in my case.I also want to make sure that if activity is paused(minimized) then, when task is completed activity is also started
Click to expand...
Click to collapse
You can still use an IntentService to do that. To stop it just pass an Intent to it with a boolean extra indicating that you don't want to do anything. You'll need only one more if clause in the onHandleIntent of the service.
SimplicityApks said:
You can still use an IntentService to do that. To stop it just pass an Intent to it with a boolean extra indicating that you don't want to do anything. You'll need only one more if clause in the onHandleIntent of the service.
Click to expand...
Click to collapse
That didnt worked I used it like this.
Code:
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
String FILENAME=intent.getStringExtra("name");
String FILEPATH=intent.getStringExtra("path");
boolean b=intent.getBooleanExtra("run",false);
while(b){
ArrayList<File> a=getSearchResult(new File(FILEPATH),FILENAME);
publishResults(a);
this.stopSelf();}
}
Code:
final Intent intent = new Intent(getActivity(), SearchService.class);
intent.putExtra("path",fpath);
intent.putExtra("name",a);
intent.putExtra("run",true);
p=new ProgressDialog(getActivity());
p.setCancelable(false);
p.setTitle("Searching Files");
p.setMessage("Please Wait");
p.getWindow().addFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON );
p.setButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface p1, int p2)
{
Intent j=new Intent(getActivity(),SearchService.class);
j.putExtra("run",false);
getActivity().stopService(j);
// TODO: Implement this method
}
});
p.show();
getActivity().startService(intent);
Sent from my GT-S5570 using XDA Premium 4 mobile app
arpitkh96 said:
That didnt worked I used it like this.
Code:
@Override
protected void onHandleIntent(Intent intent) {
// TODO Auto-generated method stub
String FILENAME=intent.getStringExtra("name");
String FILEPATH=intent.getStringExtra("path");
boolean b=intent.getBooleanExtra("run",false);
while(b){
ArrayList<File> a=getSearchResult(new File(FILEPATH),FILENAME);
publishResults(a);
this.stopSelf();}
}
Code:
final Intent intent = new Intent(getActivity(), SearchService.class);
intent.putExtra("path",fpath);
intent.putExtra("name",a);
intent.putExtra("run",true);
p=new ProgressDialog(getActivity());
p.setCancelable(false);
p.setTitle("Searching Files");
p.setMessage("Please Wait");
p.getWindow().addFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON );
p.setButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface p1, int p2)
{
Intent j=new Intent(getActivity(),SearchService.class);
j.putExtra("run",false);
getActivity().stopService(j);
// TODO: Implement this method
}
});
p.show();
getActivity().startService(intent);
Click to expand...
Click to collapse
First, if you look in the Dokumentation for IntentService, it says that you should not call stopSelf because it is already implemented to do that when there are no intents left. So It really should be easier to use a Service if you want to stop it like that.
If you want to keep using the intent service, I'd instead use a boolean instance variable which is checked in the publishResults method so just let the service do its work, but before it is published in the UI thread check if the dialog was canceled or not. Otherwise because you have two threads you can't be sure when the other thread receives the boolean change.
To me it seems like you could also use an AsyncTask to handle the threading and that class is easily cancelable .
SimplicityApks said:
First, if you look in the Dokumentation for IntentService, it says that you should not call stopSelf because it is already implemented to do that when there are no intents left. So It really should be easier to use a Service if you want to stop it like that.
If you want to keep using the intent service, I'd instead use a boolean instance variable which is checked in the publishResults method so just let the service do its work, but before it is published in the UI thread check if the dialog was canceled or not. Otherwise because you have two threads you can't be sure when the other thread receives the boolean change.
To me it seems like you could also use an AsyncTask to handle the threading and that class is easily cancelable .
Click to expand...
Click to collapse
I cannot use Asynctask ,as operation could be long.checking the boolean before publish is good idea.I will try this
Sent from my GT-S5570 using XDA Premium 4 mobile app

[Q] Help with starting an activity

Could you help. I am trying to make an app that when a button is pressed it opens a certain activity in one app, a different one. How would you make that. I am stuck. It can be hard coded. I am not home now but later I will post the code I have so far. Thanks.
JonanomisK said:
Could you help. I am trying to make an app that when a button is pressed it opens a certain activity in one app, a different one. How would you make that. I am stuck. It can be hard coded. I am not home now but later I will post the code I have so far. Thanks.
Click to expand...
Click to collapse
Code:
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(context, NewActivity.class));
}
});
Simples
Jonny said:
Code:
Button button = (button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(context, NewActivity.class));
}
});
Simples
Click to expand...
Click to collapse
Thanks! I'll try implementing that later.

Categories

Resources