Hi,
the following code shows a notification in the menu bar. It works if I launch it from an activity, but it doesn't work if I launch it from a service (which runs when I receive BOOT_COMPLETED).
Code:
private void showNotification(String msg) {
String menuText = msg;
String contentTitle = getString(R.string.notify_title_safety);
String contentText = msg;
Context context = getApplicationContext();
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new Notification(R.drawable.ico_notification, menuText, System.currentTimeMillis());
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, notification);
}
I can't find any informations about why it works from the activity and not from a service. In fact, in the very first lines of the official docs it is suggested that Services use Notifications instead of Activities (which already have the attention of the user).
Any help/previous experience?
Dumb mistake: the code was not being called because the service name was missing the leading "." in AndroidManifest.xml
Related
The code below draws my png images. Now I want to get left_btn clickable. When you click on the image left_btn should left_foot be hidden and another png image appear. How do you make these? I am not useing layout -> main.xml
Code:
public class DrawView extends View implements OnClickListener {
private ColorBall game, left_btn, right_btn, head_body, left_foot, right_foot, left_arm, right_arm, left_hand, right_hand;
public DrawView(Context context) {
super(context);
setFocusable(true); //not yet necessary, but you never know what the future brings
// declare each ball with the ColorBall class
game = new ColorBall(context,R.drawable.test1);
left_btn = new ColorBall(context,R.drawable.left_btn);
right_btn = new ColorBall(context,R.drawable.right_btn);
head_body = new ColorBall(context,R.drawable.head_body);
left_foot = new ColorBall(context,R.drawable.left_foot);
right_foot = new ColorBall(context,R.drawable.right_foot);
left_arm = new ColorBall(context,R.drawable.left_arm);
right_arm = new ColorBall(context,R.drawable.right_arm);
left_hand = new ColorBall(context,R.drawable.left_hand);
right_hand = new ColorBall(context,R.drawable.right_hand);
}
@Override protected void onDraw(Canvas canvas) {
canvas.drawBitmap(game.getBitmap(), 0, 0, null);
canvas.drawBitmap(left_btn.getBitmap(), 45, 206, null);
canvas.drawBitmap(right_btn.getBitmap(), 397, 206, null);
canvas.drawBitmap(head_body.getBitmap(), 230, 132, null);
canvas.drawBitmap(left_foot.getBitmap(), 225, 185, null);
canvas.drawBitmap(right_foot.getBitmap(), 250, 185, null);
canvas.drawBitmap(left_arm.getBitmap(), 205, 165, null);
canvas.drawBitmap(right_arm.getBitmap(), 260, 165, null);
canvas.drawBitmap(left_hand.getBitmap(), 190, 145, null);
canvas.drawBitmap(right_hand.getBitmap(), 290, 145, null);
invalidate();
}
Set an OnClickListener on left_btn and set a variable to true so you draw the original if false or the replacement if true. Also, why are you calling invalidate in onDraw()? Invalidate will force a redraw. Generally you don't want that until something changes that requires a redraw.
Hi guys,
I am totally new at this. I mean completely raw .
I successfully put together a searchlight project by following a tutorial on youtube.
Now I want to do some work on a project that will fetch information from my employers website into the app. This information will be inform of text and images.
Here's the code I'm using in my attempt to read the remote text:
Code:
try {
URL url = new URL( "fullpath-url-to-file-dot-php" );
URLConnection conn = url.openConnection();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = "";
while ((line = rd.readLine()) != null) {
Message lmsg;
lmsg = new Message();
lmsg.obj = line;
lmsg.what = 0;
Context context = getApplicationContext();
CharSequence text = "Hello toast! "+lmsg;
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);toast.show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
Context context = getApplicationContext();
CharSequence text = "error!";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);toast.show();
}
I admit I barely know what I am doing. My attempt to download a simple line of text from a website url keeps failing.
Anyway, Can you guys give me an idea what I am doing wrong and what I should be doing instead?
Thanks
Update
I found out what the nature of the exception is.
It's a
Code:
java.net.socketexception: permission denied
exception.
Can anyone please shed some light on this for me ?
Did you add the internet permission to your manifest file?
Should be something like
<uses-permission>android.permission.INTERNET</uses-permission>
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?
I need to create a dialog with a list containing an autocomplete text view and a button for each line. So everything is working except for the autocomplete text view. My program crashes when i tried to call the setAdapter. that's my code:
Code:
private Dialog createSosteDialog() {
final ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
Button conferma_modifiche_soste;
ListView lv;
final Dialog sosteDialog = new Dialog(this);
sosteDialog.setTitle("Lista soste");
sosteDialog.setContentView(R.layout.modifica_soste_dialog_layout);
lv=(ListView)sosteDialog.findViewById(R.id.lista_write);
conferma_modifiche_soste=(Button)sosteDialog.findViewById(R.id.Conferma_modifiche_soste);
AutoCompleteTextView autocomplete=(AutoCompleteTextView)sosteDialog.findViewById(R.id.autocomplete_soste);
String[] fermate_dispositivo = getResources().getStringArray(R.array.Lista_fermate_dispositivo);
ArrayAdapter<String> adapter_autocomplete = new ArrayAdapter<String>(this, R.layout.auto_complete_item, fermate_dispositivo);
autocomplete.setAdapter(adapter_autocomplete); //ERROR WITH THIS LINE!!!!
HashMap<String, String> map;// = new HashMap<String, String>();
int i;
for(i=0;i<soste.length;i++){
map = new HashMap<String, String>();
map.put("sosta", soste[i].getNome());
map.put("orario", soste[i].getStringOrario());
mylist.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this, mylist, R.layout.lista_soste_item_modifica,
new String[] {"sosta", "orario"},
new int[] {R.id.autocomplete_soste, R.id.lista_soste_cambia_orario});
//new String[] {"from", "to"} imposta le colonne "sosta" e "orario" della map come valori
lv.setAdapter(adapter);
return sosteDialog;
}
I don't know if this is the best way to do that, by the way i've to make a dialog with 1 autocomplete and a button for each line of the list inside that dialog. how could i do that?
thx everyone for your help!
any ideas?
i really need your help, maybe it's a problem with the SimpleAdapter?!?
Hi, I have a newbie question..
I have modified the bluetoothChat example to get serial data from my pc to my phone over bluetooth.
The code uses the following code to keep listening for incoming data:
Code:
public void run() {
Log.i(TAG, "BEGIN mConnectedThread");
byte[] buffer = new byte[1024];
int bytes = 0;
// byte x = 0;
// Keep listening to the InputStream while connected
while (true) {
try {
// Read from the InputStream
bytes = mmInStream.read(buffer);
// Send the obtained bytes to the UI Activity
mHandler.obtainMessage(BluetoothChat.MESSAGE_READ, bytes, -1, buffer)
.sendToTarget();
} catch (IOException e) {
Log.e(TAG, "disconnected", e);
connectionLost();
// Start the service over to restart listening mode
BluetoothChatService.this.start();
break;
}
}
}
And in the MainActivity the following code when data is received:
Code:
case MESSAGE_READ:
byte [] readBuf;
//readBuf = (byte[]) msg.obj;
readBuf = (byte[]) msg.obj;
String readMessage = new String(readBuf, 0, msg.arg1); // create string from bytes array
messageString = messageString + readMessage;
if (messageString.length()<10){
TextView text = (TextView) findViewById(R.id.textView1); // Display output
text.setText("<10....");
break;
}
sb.append(readMessage); // append string
int endOfLineIndex = sb.indexOf("#"); // determine the end-of-line
if (endOfLineIndex > 0) { // if end-of-line,
String sbprint = sb.substring(0, endOfLineIndex); // extract string
sb.delete(0, sb.length()); // and clear
TextView text = (TextView) findViewById(R.id.textView1); // Display output
text.setText(sbprint);
mConversationArrayAdapter.add(mConnectedDeviceName+": " + readMessage);
}
break;
As shown i have added a endOfLineIndex loop to find the end of the received string and show the received string in a textfield. The problem with my code is when sending a string like "0123456789#" the shown text in the textView1 is mostly not the send string.. I get strings like 0123,4560123456789, etc.. Somehow there is still data in the buffer i quess.
Is there a way to clear or ignore the received data and only parse the wanted string form the buffer? If this is possible i can add a start identifier to the string..
The main goal is too parse a string like: #150,000,001,000,0.0,-0.1,40.3,144.0,001,OKE where each value is a variable which needed to be placed in a textview.
Thanks for any suggestions!
Kind regards,
Bas
This one "0123456789#" should return "0123456789". Does it?
You invoke substring().
Quote from the docs:
The substring begins at the specified beginIndex and extends to the character at index endIndex - 1.
Click to expand...
Click to collapse
(http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#substring(int, int))
That means that the last character (the #) is missed out.
Thanks Nikwen,
I do miss out the end off string character #. The problem is i get different strings out of the buffer... If i send 0123456789# with a one second interval i get different results.. sometimes i get the string like expected which is 0123456789. but most of the time i get results like 0123, 456789,12,1,etc etc. it looks like the buffer is not cleared or something