I am getting the following error in service class. What is the issue here ?
Error:(17, 8) error: NotificationService is not abstract and does not override abstract method onBind(Intent) in Service
This is my code :
package works.viswajith.birthdayreminder;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.support.v4.app.NotificationCompat;
import java.util.Calendar;
/**
* Created by Viswajith on 6/17/2015.
*/
public class NotificationService extends Service {
DBHelper db;
private Cursor cur;
private Calendar cal;
private int d,m,y;
private String[] temp;
private NotificationManager nm;
int i=0;
#Override
public void onCreate() {
super.onCreate();
db=new DBHelper(getApplicationContext());
cur=db.getDetails();
cal= Calendar.getInstance();
d=cal.get(Calendar.DAY_OF_MONTH);
m=cal.get(Calendar.MONTH);
y=cal.get(Calendar.YEAR);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
for(i=0;i<db.getCount();i++)
{
temp=cur.getString(2).split("/");
if(Integer.parseInt(temp[0])==d)
{
NotificationCompat.Builder mbuilder=new NotificationCompat.Builder(this);
mbuilder.setSmallIcon(R.drawable.happy);
mbuilder.setContentTitle("Happy B'day "+cur.getString(0));
mbuilder.setContentText("Today is "+cur.getString(0)+"'s B'day , Wish him ..");
nm=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(100,mbuilder.build());
}
cur.moveToNext();
}
return super.onStartCommand(intent, flags, startId);
}
}
The error has explained it to you perfectly clear: You have to override onBind() or otherwise make your service abstract.
This is how you should do it:
#Override
public IBinder onBind(Intent intent) {
// Implement your logic here.
}
It is telling you exactly what you need to do...
Because you are extending a service, you need to override the onBind method.
Add the following to your class:
#Override
public IBinder onBind(Intent intent) {
}
This method will be called when android binds to your services.
Related
Basically i have created alarm app but the broadcastReceiver not getting called in the marshmallow i know about the runtime permission required in the marshmallow but i don't know what will be the permission for my problem for calling broadcastReceiver.
Can anybody please suggest me something please.
I simply solve this problem by creating a service by following this link
Link
Service Code Here...
import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
/**
* Created by waqar on 1/05/2017.
*/
public class LocalService extends Service {
AlarmReceiver alarmReceiver;
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
alarmReceiver = new AlarmReceiver();
IntentFilter screenStateFilter = new IntentFilter();
screenStateFilter.addAction(Intent.ACTION_SCREEN_ON);
screenStateFilter.addAction(Intent.ACTION_SCREEN_OFF);
this.registerReceiver(alarmReceiver, screenStateFilter);
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(alarmReceiver);
}
}
And start this service from MainActivity in onCreate Method.
Intent intent = new Intent(getApplicationContext(), LocalService.class);
getApplicationContext().startService(intent);
And don't forget to register this service in the manifest.
Is it that the value of TAG be always the name of the package in android studio as shown below?I get a problem as logging tag can be atmost 23 characters??
package com.example.swangmo.intentexample;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
private static final String TAG="com.example.swangmo.intentexample";
public MyService() {
}
#Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
#Override
public void onDestroy() {
Log.i(TAG,"onDestroy method called");
}
}
You can use any string in TAG not necessarily package name. But the condition is you have to keep it below 24 characters i.e. at max 23 characters, not more than that.
The TAG is just a (static final) String, set it to whatever you want that makes it easier for you to parse the runtime log.
I have created a Service in my android application which starts running on BOOT_COMPLETE. I want to run my Service non-stop (run always), and for that I have used while(true) inside onStartCommand() method. So is this fine to use while(true) or there is any other better way to run a service always in background?
This is code of my Service:
package com.example.abc.project1;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import org.json.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class HelloService extends Service {
private static final String TAG = "HelloService";
private boolean isRunning = false;
#Override
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public void onCreate() {
super.onCreate();
isRunning = true;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Thread(new Runnable() {
#Override
public void run() {
while(true) {
/*non-stop work to be done in background always*/
}
}
}).start();
return Service.START_STICKY;
}
#Override
public void onDestroy() {
isRunning = false;
}
}
I have not tried this myself but if you change receiver to service it should work.
I'm trying to use a Content Observer to update a Service when any changes happen to the sqlite database in my app.
I'm confused as to what to do, so I put together some code below. Usually, Content Observers are used with contacts or mediaplayer with a background service. In my research I read that it can be used with the sqlite database on the phone.
Questions:
1. Since Sqlite database does not have a uri, what info do I replace People.CONTENT_URI in
this.getContentResolver().registerContentObserver (People.CONTENT_URI, true, contentObserver);
2. In my research I didn't find any code that would go into the database class that would alert the ContentObserver. Does all the code for the Content Observer work within the service class?
Note that this question is similar to Android SQLite DB notifications and
how to listen for changes in Contact Database
Both questions do not explicitly answer my question. If you have code that explains this, that would be very helpful.
Here is my semi-pusedo code below. It does not work. I'm using it to learn about how to update a service when the database info changes.
package com.example.com.test.content.observer;
import java.sql.Date;
import java.util.Calendar;
import java.util.List;
import com.google.android.gcm.demo.app.Alerts.AlarmsService;
import com.google.android.gcm.demo.app.Alerts.Alerts;
import com.google.android.gcm.demo.app.sqllite.DatabaseSqlite;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.provider.Contacts.People;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.database.ContentObserver;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
public class AlarmService extends Service
{
Handler mHandler = new Handler();
DatabaseSqlite db = new DatabaseSqlite(this);
List<Alerts> listAlerts;
PendingIntent pendingIntent;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.getApplicationContext()
.getContentResolver()
.registerContentObserver(?????, true,
contentObserver);
}
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("TAG", "started onstart command Created from Alerts service .");
return super.onStartCommand(intent, flags, startId);// START_STICKY;
}
#Override
public void onStart(final Intent intent, int startId) {
super.onStart(intent, startId);
runThread();
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show();
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
private class MyContentObserver extends ContentObserver {
#SuppressLint("ParserError")
public MyContentObserver(Handler mHandler) {
super(mHandler);
}
#Override
public void onChange(boolean selfChange) {
runThread();
super.onChange(selfChange);
}
public void runThread(){
Thread thread = new Thread() {
#Override
public void run() {
Boolean x = true;
while (x) {
db.open();
listAlerts = db.getAlarmsForService();
db.close();
int alerts=listAlerts.size();
for (int i = 0; i < alerts; i++) {
Alerts item = listAlerts.get(i);
item.getRowId();
item.getRemoteServerId();
String alertInMills = item.getAlertDateInMills();
String alertDuration = item.getAlertDurationInMinutes();
String eventName = item.getEventName();
long longAlertInMills = Long.parseLong(alertInMills);
pendingIntent = PendingIntent.getService(AlarmsService.this, 0,intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
// go to data base for time in mills
calendar.setTimeInMillis(longAlertInMills);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
pendingIntent);
//
System.out.println(calendar.toString());
}
//
System.out.println("thread");
x = false;
}
}
};
thread.start();
}
}
MyContentObserver contentObserver = new MyContentObserver(mHandler);
this.getContentResolver().registerContentObserver (People.CONTENT_URI, true, contentObserver);
}
In general, there are two parts to handling this: you have the ContentObserver which needs to register to receive changes, as you've pointed out, and the SQLiteDatabase which has to notify the registered observers of any changes. If this is a database you own, you can create the URI that you can use to listen for.
(1) First define your URI, typically in your Database definition file.
public static final Uri CONTENT_URI = Uri.parse("mycontent://packagename/something");
(2) for your database Content Provider:
Each db function (insert, update, delete) should call through to notifyChange() after completing the operation in order to inform the observers that changes have happened.
rowId = db.insert(tableName, null, cv);
...
getContext().getContentResolver().notifyChange(newUri, null);
(3) Create and register your ContentObserver in the Service, as described in the same link you provided above (remember to override the deliverSelfNotifications() to return true)
public class MyService extends Service {
private MyContentObserver mObserver;
#Override
public void onStartCommand(Intent intent, int flags, int startId) {
...
mObserver = new MyContentObserver();
getContentResolver().registerContentObserver(Dbfile.CONTENT_URI, null, mObserver);
}
#Override
public void onDestroy() {
...
if (mObserver != null) {
getContentResolver().unregisterContentObserver(mObserver);
mObserver = null;
}
}
// define MyContentObserver here
}
(4) In your ContentObserver.onChange(), you can post something to the Service or handle the change right there if possible.
Also, if it helps your cause, you can customize the URI definition to handle different types of data that you are observing, register your observer for each URI, and then override ContentObserver.onChange(boolean, Uri) instead.
Hope this helps!
I'm trying to create a service which will start by the user request in the application.
After the user will choose an update interval, the service will run in the operation system background, and will send a non-relevant message.
I've tried to write the service according to the example for Service class API.
For some reason, I figured in debug (when running doBindService() method) that mUpdateBoundService is getting null.
My second question is whether I can use "Toast" inform message outside an application ? (As kind of a desktop notification).
Can anyone help ? Here is my short code:
UpdateService.java
package android.update;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;
public class UpdateService extends Service {
private NotificationManager mNM;
private final IBinder mBinder = new UpdateBinder();
private int updateInterval;
public class UpdateBinder extends Binder {
UpdateService getService() {
return UpdateService.this;
}
}
public void onCreate() {
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Timer timer = new Timer();
timer.schedule(new UpdateTimeTask(), 100, updateInterval);
}
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
class UpdateTimeTask extends TimerTask {
public void run() {
showNotification();
}
}
public void showNotification() {
Toast.makeText(this, "Hi", 10);
}
#Override
public IBinder onBind(Intent intent) {
updateInterval = intent.getExtras().getInt(getString(R.string.keyUpdateInterval));
return mBinder;
}
}
UpdateActivity.java
package android.update;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class UpdateActivity extends Activity {
private UpdateService mUpdateBoundService;
private boolean mIsBound = false;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClickStartUpdateService(View view) {
switch (view.getId()) {
case R.id.btnStartUpdateService:
doBindService();
//Toast.makeText(this,"Service Started",Toast.LENGTH_LONG).show();
mUpdateBoundService.showNotification();
break;
}
}
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
mUpdateBoundService = ((UpdateService.UpdateBinder)service).getService();
}
public void onServiceDisconnected(ComponentName className) {
mUpdateBoundService = null;
}
};
private void doBindService() {
Intent updateActivityIntent = new Intent(UpdateActivity.this,
UpdateService.class);
EditText txtUpdateInterval = (EditText) findViewById(R.id.txtUpdateInterval);
int interval = Integer.parseInt(txtUpdateInterval.getText().toString());
updateActivityIntent.putExtra(getString(R.string.keyUpdateInterval), interval);
bindService(updateActivityIntent, mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
}
void doUnbindService() {
if (mIsBound) {
unbindService(mConnection);
mIsBound = false;
}
}
#Override
protected void onDestroy() {
super.onDestroy();
doUnbindService();
}
}
Your toast is not showing because you are not telling it to. Try:
public void showNotification() {
Toast.makeText(this, "Hi", 10).show();
}
For your service issue, I think that you do not properly understand how services & activities work together. A service can run independently of a service, or you can have a service whose lifecycle matches that of a given activity. From your code, it is not clear which of these models you are following. Your implementation will cause the service to wake periodically, but only while your activity is running. If the user switches to another activity, your service will no longer be woken.
If you want a service to wake periodically independently of the activity, then you need to run your timer event in the service itself. Better still use an Alarm to wake your service: Register an Alarm with AlarmManager which will fire an Intent at a future point (or regular intervals, if you prefer), and extend your service from IntentService, override onHandleIntent() and add the necessary Intent Filter to your Service entry in the manifest.