2
I’m creating a app simple that gets a notification every time I click a button on my layout, everything works perfectly. Now I want this notification to be repeated for example every minute until I deactivate it. Can someone give me a hint as to how I can do this?? :).
Here is my code.
public class MainActivity extends Activity {
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.main);
Button createNotification = (Button) findViewById
(R.id.create_notification_button);
createNotification.setOnClickListener (new View.OnClickListener() {
@Override
public void onClick (View v) {
Intent intent = new Intent (MainActivity.this ,
notificationActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity
(MainActivity.this, 0, intent, 0);
Notification notification = new Notification.Builder (MainActivity.this)
.setContentTitle(getString(R.string.new_notification))
.setContentText(getString (R.string.notification_content))
.setSmallIcon (R.drawable.ic_launcher)
.setContentIntent(pendingIntent)
.getNotification();
notification.flags = Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager =
(NotificationManager) getSystemService (NOTIFICATION_SERVICE);
notificationManager.notify (0, notification);
}
});
}
}
Thanks... I’ll give you to analyze the code any doubt I warn...
– Yanik Santos
I have made all the modifications recommended by you by logging in when clicking does not enter any notification. No Logcat stops the following message "Skipped 47 frames! The application may be Doing Too Much work on its main thread.".
– Yanik Santos
The code was tested before posting it. I don’t understand this message in this context, since the service code (launch notification) does not run on main thread. Test the service with the example Activity I posted.
– ramaral
I used the example Activity and the lines . setContentTitle(getString(R.string.new_notification)) . setContentText(getString (R.string.notification_content)) appear with error in new_notification and notification_content. What should I do there??
– Yanik Santos
This is the code that creates the notification, it was copied from your question. It is supposed that these string Resources have been created by you. For testing you can replace
getString(R.string.xxxx)
for"qualquer coisa"
– ramaral
I removed the error but still no notification sent... If you can send the project you have tested to my email please... [email protected]
– Yanik Santos
You declared the service on Androidmanifest.xml?
– ramaral
Yes I did declare..
– Yanik Santos
Note that it has to be declared within the tag
<application>
.– ramaral
@Amaral you’re the guy, it turns out I had opened another Aplication tag and had not noticed. Now it’s working perfectly. Thank you so much for your help and patience.
– Yanik Santos
Switching to +1. = D
– viana