Thread to update notification - Android Software Development

Hello
I have a problem with my new application development.
I write little task manager and I want to create notification in the status bar that will show the number of the running process in the system.
The notification will update every 60 seconds.
in order to make the update I created new thread in the background wich will update the notification. My problem is that my thread must extents Activity class (to get the number of running proccess).
and I could get this value only when the Activity in "Active" status.
when the activity in the background this value isnt updated.
what can I do?
thanks and sorry for my poor english
this is my example code - refresh every 60 seconds.
I want to change the text "Click here to open Tablet popup manager" to dynamic number of proccess.
Code:
new Thread(new Runnable() {
public void run()
{
int icon = R.drawable.icon;
long when = System.currentTimeMillis();
while(true)
{
android.os.SystemClock.sleep(60000);
managerActivity.notification = new Notification(icon, "", when);
CharSequence contentTitle = "Task manager";
CharSequence contentText= "Click here to open Tablet popup manager";
if(MainPopup.taskNames!=null){
contentText = String.valueOf(MainPopup.numOfTasks)+ " Tasks";}
managerActivity.notification.flags |= Notification.FLAG_NO_CLEAR;
if(managerActivity.notification!=null)
{
managerActivity.notification.setLatestEventInfo(getApplicationContext(), contentTitle, contentText, contentIntent);
mNotificationManager.notify(1,managerActivity.notification);
}
}
}
}).start();

please someone ?!
I really need this help

Related

android programming question

i'm not sure if this is the right place to ask this, so please direct me to the correct place if this is wrong.
I have an app that is constantly updated a notification in the status bar (changing the icon and updated text in the expanded view). The problem I am having is that when it updates the notification it will close the window I am in for certain actions. For example, if I am looking through my apps when it updates it will close that and take me to the home screen, if I am searching for something in the market it will close the search history, etc. Is there a way to have it update the icon and text in the background and not effect any currently running activity?
here is my notification code:
public void displayNotification(String msg)
{
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
settings = context.getSharedPreferences(PREFS_NAME, 0);
statColor = settings.getInt("txtColor", Color.BLACK);
contentView.setTextColor(R.id.text, statColor);
contentView.setTextColor(R.id.text2, statColor);
contentView.setTextColor(R.id.text3, statColor);
contentView.setTextViewText(R.id.text, "some text");
contentView.setTextViewText(R.id.text2, "some more text");
contentView.setTextViewText(R.id.text3, "some more text");
contentView.setImageViewResource(R.id.image, icon[iconIndex]);
Notification notification = new Notification(icon[iconIndex], msg, System.currentTimeMillis());
notification.contentView = contentView;
Intent notificationIntent = new Intent(this, AppService.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 1, notificationIntent, 0);
notification.contentIntent = contentIntent;
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notification.flags |= Notification.FLAG_NO_CLEAR;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
manager.notify(NOTIFICATION_ID, notification);
}
this thread can be deleted, i fixed it
Considering the
is it possible for you to tell us what you changed to fix it?

[Q] update view of activity that implements onClickListener without needing to click

How do you update an activity that implements on click listener WITHOUT needing to click something? (e.g. with a timer)
What is it you are trying to do? Why the new thread?
I need to update button, background images and text based on a timer instead of only updating when something is clicked
I don't understand what you mean by "only updating when something is clicked."
the images used (e.g. back ground, or buttons) will not change (update) unless a button is clicked. So its basically waiting for something to be pressed (any button) before it updates any imgaes
So let me get this straight; you're changing the images associated with layout controls in a timer of some kind? What kind of timer implementation? Is the timer in its own thread?
yeah. I tried making another class for the time but it doesn't work for updating the view of the main activity that I want. So I am trying to use:
Timer timer2 = new Timer();
timer2.scheduleAtFixedRate(Title(), 1, 1000);
Title() is defined as: public TimerTask Title()
NEVERMIND!! I GOT IT!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
TimerTask scanTask;
final Handler handler = new Handler();
Timer t = new Timer();
scanTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
// stuff to do
}
});
}};
t.schedule(scanTask, 1, 1000);

[Q] DatePickerDialog cancelclick

Hi there!
I have been trying to catch the Cancel click of a DatePickerDialog, because I want to do some additional stuff, when the user clicks on the Cancel Button.
I tried it like described in the second answer from esilver from this Question:
http://stackoverflow.com/questions/...erner-of-datepicker-dialog?tab=active#tab-top
But I can't get it to work like that. When do I have to call this onClick method?
Would be great if someone could help me with that!
Thanks!
cTrox said:
Hi there!
I have been trying to catch the Cancel click of a DatePickerDialog, because I want to do some additional stuff, when the user clicks on the Cancel Button.
I tried it like described in the second answer from esilver from this Question:
http://stackoverflow.com/questions/...erner-of-datepicker-dialog?tab=active#tab-top
But I can't get it to work like that. When do I have to call this onClick method?
Would be great if someone could help me with that!
Thanks!
Click to expand...
Click to collapse
the "checked" solution in that example seems wrong to me. but the second one people voted up seems correct.
You can also set the onDissmissListener which will catch if the user backs out with the back key ( recommended for user friendliness )
have a look here:
http://developer.android.com/refere...id.content.DialogInterface.OnDismissListener)
Also, since DatePickerDialog is a subclass of AlertDialog, you can set the buttons the same way:
http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog
That should get you started but feel free to post back if you get stuck again. And post the code you are using.
Also, one other thing, it might be useful to keep a private reference to your dialog in your activity class.
All those examples (in the API docs and tutorials) always show a new dialog created when "onCreateDialog(int ID)" is called by the OS on your activity and they never save any sort of reference to it. They give you just enough code to hang yourself
Anyways, while this is a perfectly normal way to do things, it doesnt give you a chance to follow what is actually happening with the dialog. It also makes it harder to reference your dialog from elsewhere in the activity.
Keeping a reference, and exploring the onPrepareDialog(int ID) method are good for learning what the OS is doing with your dialog. (IMHO)
hth
Thanks a lot for your answers. But I still can't figure out how to do it.
Here's my current Code:
Code:
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker datePicker, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
// do some more stuff...
}
};
Code:
protected Dialog onCreateDialog(int id) {
Calendar cDate = Calendar.getInstance();
int cyear = cDate.get(Calendar.YEAR);
int cmonth = cDate.get(Calendar.MONTH);
int cday = cDate.get(Calendar.DAY_OF_MONTH);
switch(id){
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, cyear, cmonth, cday);
}
return null;
}
With that I can just call showDialog(DATE_DIALOG_ID); and I get the dialog. Now, where do I have to implement this OnDismissListener and how?
Thanks!
there are lots of ways to do this but I broke it out into several parts so hopefully it seems more obvious what is happening.
Code:
//here's our field reference we could use later or reuse or whatever
private DatePickerDialog dateDialog = null;
protected Dialog onCreateDialog(int id)
{
//your calendar code here... just removed to save space
switch(id)
{
case DATE_DIALOG_ID:
dateDialog = new DatePickerDialog(this, mDateSetListener, cyear, cmonth, cday);
dateDialog.setButton ( DialogInterface.BUTTON_NEGATIVE, android.R.string.cancel, cancelBtnListener );
dateDialog.setOnDismissListener ( listener );
break;
}
return dateDialog;
}
//our dismiss listener
protected DialogInterface.OnDismissListener dismissListener = new OnDismissListener( )
{
@Override
public void onDismiss ( DialogInterface dialog )
{
// do your thang here
}
};
//our click listener
protected DialogInterface.OnClickListener cancelBtnListener = new OnClickListener( )
{
@Override
public void onClick ( DialogInterface dialog, int which )
{
dialog.dismiss ( );
// since we dismiss here, the next listener to get called
// is the dismiss listener. now we'll have consistent behavoir
}
};
Ah thank you very much! I was always confused, where to set the Button and the OnDismissListener.
It works perfectly like that!

[Q] ListView SimpleAdapter problem when i scroll,the progressbar move to other item

Hello,
I have a ListView , in this ListView i have a download button for each item, i have an hidden textview with a ID that i need when i click the button (the textview is filled when the listview is populating), and also i have a hidden progress bar for each item, that it appears when the downloading starts , when i click Download, i instance an asyctask, in this async task i send the progressbar object to be able to update their progress value, and the id i need
the problem is when i scroll my list , and im downloading, the progressbar appears in other item , is there any way to avoid that?
my SimpleAdapter
Code:
public View getView(int position, View convertView, ViewGroup parent) {
View row=super.getView(position, convertView, parent);
//Hidden textview
TextView videoIdText = (TextView) row.findViewById(R.id.videoId);
Button downloadButton = (Button) row.findViewById(R.id.videoDownload);
final ProgressBar downloadProgressBar = (ProgressBar) row.findViewById(R.id.downloadProgressBar);
final String videoId = videoIdText.getText().toString();
downloadButton.setOnClickListener(new OnClickListener(){
[user=439709]@override[/user]
public void onClick(View view) {
DownloadTask task = new DownloadTask();
task.setProgressBar(downloadProgressBar);
task.setVideoId(videoId);
task.execute();
}
});
return row;
}
Please post your full code, layout and manifest so I can try and debug it. Thanks.
frenzyboi said:
Please post your full code, layout and manifest so I can try and debug it. Thanks.
Click to expand...
Click to collapse
Hey there are the files, the feed_item, is the one i used to each item of the ListItem

A question about list

Hi.
I am building an app that has a list. All of the clicks on list items result in one activity, like XActivity. XActivity contains a text view. I want that when I click on ListItem1 the text view shows a string forexample string1, then onClickListItem2 it shows string2, onListItemClicX it shows stringX. If it's confusing let me know.
How can I handle it ?
Thanks in advance.
torpedo mohammadi said:
Hi.
I am building an app that has a list. All of the clicks on list items result in one activity, like XActivity. XActivity contains a text view. I want that when I click on ListItem1 the text view shows a string forexample string1, then onClickListItem2 it shows string2, onListItemClicX it shows stringX. If it's confusing let me know.
How can I handle it ?
Thanks in advance.
Click to expand...
Click to collapse
I'm assuming you're using an ArrayAdapter - it would be helpful if you posted some code.
You have to setup an OnItemClickListener for your ListView. In the OnItemClick method, you will have to get the data from the adapter using the position that was clicked. Then create an intent to ActivityX and pass the clicked data.
Code:
[B]ArrayAdapter adapter = new ArrayAdapter();[/B]
listView.setClickable(true);
//Setup the on item click listener for the listview
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Create an intent pointing to XActivity
Intent intent = new Intent(this, XActivity.class);
//Add the clicked string as an extra
intent.putExtra("KEY_CLICKEDSTRING", adapter.getItem(position - 1));
//Start the activity
this.startActivity(intent);
}
});
In ActivityX, simply retrieve the data and set it in the textview:
Code:
@Override
public void onCreate(Bundle savedInstanceState) {
... Activity setup code
TextView textView = findViewById(R.id.yourtextview);
//Get the intent data
String strClicked = getIntent().getStringExtra("KEY_CLICKEDSTRING");
textView.setText(strClicked);
}
Thank you.
But I don't know what value does the "int position" give me ? Forexample does it give the value of "1" or .... ?
I want to know that in order to know which item was clicked and now what to do .
Can anybody help me ?
Thanks in advance.
Like previously mentioned. Post your code and we can help you out better.
not complex, First I make a new ArrayAdapter<string> add adapt it to my listView. Up to know no problem. But in the continue :
// usual codes to answer a click onItemClick(AdapterView<?> av, View v, int position, long id) {
//create a intent as Alkonic said
//My problem }
...
@My problem I want to use a set of codes and understand which item was clicked. I have 41 items and my idea is this :
// getting the text of the clicked item String title = ((TextView)v).getText().toString();
If (title=getString(R.string.theTextIHadAddedToArrayAdapterForItem1)) {
// so item 1 was clicked
//Change the text of thesecond TextView in the second activity ( wich I wrote intent for)
// rewrite the if order 41 times for 41 items
...
But this doesn't work. Can anybody help me ?
@Alconic :
I don't understand the method you wrote for retrieving intent data ( I mean
//Get the intent data String strClicked = getIntent().getStringExtra("KEY_CLICKEDSTRING");
) because the "Key_CLICKEDSTRING" is always constant and one thing. How can I cange it for 41 items ?
Thank you guys anyway.
not complex, First I make a new ArrayAdapter<string> add adapt it to my listView. Up to know no problem. But in the continue :
// usual codes to answer a click onItemClick(AdapterView<?> av, View v, int position, long id) {
//create a intent as Alkonic said
//My problem }
...
@My problem I want to use a set of codes and understand which item was clicked. I have 41 items and my idea is this :
// getting the text of the clicked item String title = ((TextView)v).getText().toString();
If (title=getString(R.string.theTextIHadAddedToArrayAdapterForItem1)) {
// so item 1 was clicked
//Change the text of thesecond TextView in the second activity ( wich I wrote intent for)
// rewrite the if order 41 times for 41 items
...
But this doesn't work. Can anybody help me ?
@Alconic :
I don't understand the method you wrote for retrieving intent data ( I mean
//Get the intent data String strClicked = getIntent().getStringExtra("KEY_CLICKEDSTRING");
) because the "Key_CLICKEDSTRING" is always constant and one thing. How can I cange it for 41 items ?
Thank you guys anyway.
You should post your onItemClick routine. But for going at it blind on our side this is what I would do:
Code:
public void onItemClick(AdapterView<?> av, View v, int pos, long id) {
//Get text from a textview that is on your listview
TextView textView2 = (TextView) v.findViewById(R.id.myTextView);
String txtMytext = textView2.getText().toString();
//Create an intent pointing to XActivity
Intent intent = new Intent(this, XActivity.class);
//Add the clicked string as an extra
intent.putExtra("KEY_CLICKEDSTRING", txtMytext);
//Start the activity
this.startActivity(intent);
}
Without your code, I don't understand what you are comparing with the if statement you posted. Use this along with @Alkonic 's code should get you in the right direction. If it doesn't, we need more code to help better.
torpedo mohammadi said:
not complex, First I make a new ArrayAdapter<string> add adapt it to my listView. Up to know no problem. But in the continue :
// usual codes to answer a click onItemClick(AdapterView<?> av, View v, int position, long id) {
//create a intent as Alkonic said
//My problem }
...
@My problem I want to use a set of codes and understand which item was clicked. I have 41 items and my idea is this :
// getting the text of the clicked item String title = ((TextView)v).getText().toString();
If (title=getString(R.string.theTextIHadAddedToArrayAdapterForItem1)) {
// so item 1 was clicked
//Change the text of thesecond TextView in the second activity ( wich I wrote intent for)
// rewrite the if order 41 times for 41 items
...
But this doesn't work. Can anybody help me ?
@Alconic :
I don't understand the method you wrote for retrieving intent data ( I mean
//Get the intent data String strClicked = getIntent().getStringExtra("KEY_CLICKEDSTRING");
) because the "Key_CLICKEDSTRING" is always constant and one thing. How can I cange it for 41 items ?
Thank you guys anyway.
Click to expand...
Click to collapse
Handling Clicks:
The int position refers to the position of the item that was clicked in the arrayadapter.
Instead of this:
Code:
String title = ((TextView)v).getText().toString();
you can use
Code:
String title = adapter.getItem(position);
That code will take the clicked position and get the clicked string from the adapter.
Intent Data
When you send data with an intent, it has two parts: the Name and Value.
The following code sets the name to a constant "Key_CLICKEDSTRING" and the Value is variable, depending on what was clicked.
Code:
intent.putExtra("KEY_CLICKEDSTRING", adapter.getItem(position));
When you want to access the VALUE, you have to provide the constant name:
Code:
String strClicked = getIntent().getStringExtra("KEY_CLICKEDSTRING");
Hope that helped!

Categories

Resources