android.content.Context.getPackageName()' on a null object reference - android

I'm trying to call a service class from another service class but I get this error:
android.content.Context.getPackageName() on a null object reference
Do you know how I call service from another service?
When I setup my app to phone first, a broadcastreceiver is starting Alarm class and in Alarm class, I want to start another service in ReadGmail() method. But I get that null object reference error.
Here is my code:
public class Alarm extends Service {
private String userName;
private String password;
private String receivingHost;
Context context;
public int onStartCommand(Intent intent, int flags, int startId) {
final Handler handler = new Handler();
TimerTask doAsynchronousTask = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
String senderPassword=new String("password");
String senderUserName=new String("username#gmail.com");
Alarm newGmailClient=new Alarm();
newGmailClient.setAccountDetails(senderUserName, senderPassword);
newGmailClient.readGmail();
}
});
}
};
Timer timer = new Timer();
timer.schedule(doAsynchronousTask, 10, 120000);
return super.onStartCommand(intent, flags, startId);
};
public void setAccountDetails(String userName,String password){
this.userName=userName;//sender's email can also use as User Name
this.password=password;
}
public void readGmail(){
this.receivingHost="imap.gmail.com";//for imap protocol
Properties props2=System.getProperties();
props2.setProperty("mail.store.protocol", "imaps");
Session session2=Session.getInstance(props2, null);
try {
Store store=session2.getStore("imaps");
store.connect(this.receivingHost,this.userName, this.password);
Folder folder=store.getFolder("INBOX");//get inbox
folder.open(Folder.READ_ONLY);//open folder only to read
Message message[]=folder.getMessages();
String key= "Hey";
String subject;
for(int i=0;i<message.length;i++){
System.out.println(message[i].getSubject());
subject=message[i].getSubject();
if(subject.equals(key)){
System.out.println("inside");
Intent mTutorial = new Intent(Alarm.this, LaunchActivity.class);
this.startService(mTutorial);
//I want to call service class in here. LaunchActivity is my service class.
}
//Log.d(message[i].getSubject(),message[i].getSubject());
}
folder.close(true);
store.close();
} catch (Exception e) {
System.out.println(e.toString());
}
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d("", "FirstService destroyed");
}
}

This is your problem:
Alarm newGmailClient=new Alarm();
Your Alarm class extends Service. It is an Android Service. You cannot create an instance of an Android component with new. Only Android can create Android components. If you want to start a Service, you call startService.
Why do you want to start another Service? Please explain.

Use an interface your Service will use to communicate events:
public interface ServiceCallbacks {
void doSomething();
}

Related

Android: How to call functions in a Android service repetitively?

I have created a Android service where I am finding out the process which are in Error state and the usage stats of app from UsageStatManager. When I run this service then it executes the methods in it once and I want to do a periodic check of the process in error state and the usage stats of apps.
One way I thought of was to implement a while loop with a Thread.sleep() for the time I would like to check my statistics but wondering if there is any other way of doing this in a much better way as placing a while loop may use CPU consumption. Any ideas would be helpfull.
My code:
public class Senddata_1 extends Service {
private String ip = "85.228.204.209";
private int port = 5000;
String message;
String file;
String TAG = "Senddata_1";
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("M-d-yyyy HH:mm:ss");
public void find_out_process_in_error_state(){
/*Some code to find out process in error state*/
return;
}
private UsageStatsManager getUsageStatsManager(Context context){
UsageStatsManager usm = (UsageStatsManager) context.getSystemService("usagestats");
return usm;
}
public List<UsageStats> getUsageStatsList(Context context){
List<UsageStats> usageStatsList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY,startTime,endTime);
return usageStatsList;
}
public void printUsageStats(List<UsageStats> usageStatsList){
}
#Override
public void onCreate() {
super.onCreate();
Log.e(TAG, "Inside service Senddata_1");
find_out_process_in_error_state();
printUsageStats(getUsageStatsList(Senddata_1.this));
new Thread(new Senddata_1.ClientSend()).start();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
#Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
// return;
}
public class ClientSend implements Runnable {
}
}
you can use Timer and in there set time to start service and run function
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
//start service here
}
}, 0, mTimeRate);
mTimeRate will be in milliseconds

Application skipping 600 frames

so im building this service for a application locker. it runs fine for the most part.but when i try to run the service to lock my own application(ie the app locker itself) there's a lag for like 4-5 seconds and then the lock activity launches. The logcat displays that it has skipped 600 frames and is doing too much work on the main thread. can anyone tell him how do i fix this or optimize this code
the AppActivities contains the name of activities that are to be ignored from launching the locker again when they are on top of the stack.eg the lockscreen activity to be shown to the user. The allowedapp is the last app verified by the user
public class LockerService extends Service {
String LockedApps[];
String allowedapp = null;
DataBaseHandler handler;
Intent pwdIntent = null;
ActivityManager am;
String[] AppActivities = { "com.packagename.Locker",
"com.packagename.Compare_Pattern",
"com.packagename.Captcha_Verfication",
"com.haibison.android.lockpattern.LockPatternActivity" };
private final static Handler servicehandler = new Handler() {
#Override
public void handleMessage(Message msg) {
}
};
#Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
handler = new DataBaseHandler(this);
am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
pwdIntent = new Intent(LockerService.this, Locker.class);
pwdIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
private Runnable checkforeground = new Runnable() {
public void run() {
handler.open();
LockedApps = handler.getPackages();
handler.close();
String packname = am.getRunningTasks(1).get(0).topActivity
.getPackageName();
String activityname = am.getRunningTasks(1).get(0).topActivity
.getClassName();
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(LockerService.this);
allowedapp = sp.getString("allowedapp", "anon");
// check if top application is mylocker application
if ((packname.equals("com.packagename"))
&& (allowedapp.equals("com.packagename"))) {
// do nothing
}
// check if top application is mylocker application and prevent relaunching the lockeractivity every 1.5 seconds
else if ((packname.equals("com.packagename"))
&& !(Arrays.asList(AppActivities).contains(activityname))) {
try {
Editor edit = sp.edit();
edit.putString("current_app", packname);
edit.commit();
startActivity(pwdIntent);
Thread.sleep(10000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if ((Arrays.asList(LockedApps).contains(packname))
&& (allowedapp.equals(packname))) {
// do nothing
} else if ((Arrays.asList(LockedApps).contains(packname))) {
Editor edit = sp.edit();
edit.putString("current_app", packname);
edit.commit();
startActivity(pwdIntent);
}
servicehandler.postDelayed(this, 1500); // 1.5 seconds
}
};
#Override
public void onStart(Intent intent, int startId) {
servicehandler.removeCallbacks(checkforeground);
servicehandler.postDelayed(checkforeground, 1500);// 1.5 second
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
servicehandler.removeCallbacks(checkforeground);
stopSelf();
}
}
first of all as Gabe mentioned, a runnable runs on the main Thread.To solve the frames issue You'll need to create another new thread to run your code in the background.
Try this initialize executorService and LcThread and a boolean running_statusin your service.
The running_status variable is used to break the while loop of your thread so that stops looping in the back
#Override
public void onStart(Intent intent, int startId) {
running_status = true;
executorService = Executors.newSingleThreadExecutor();
servicehandler.removeCallbacks(LcThread);
LcThread = new LockerThread();
executorService.submit(LcThread);
}
create the following class
class LockerThread implements Runnable {
#Override
public void run() {
while(running_status){
//copy code from your old Runnable run method here
}
}
}
next modify the onDestroy method
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (executorService != null) {
executorService.shutdown();
}
running_status = false;
servicehandler.removeCallbacks(LcThread);
stopSelf();
}
hope this solves your problem
A runnable still happens on the main thread. Services do not have their own thread by default, they run on the UI thread. If you want to do heavy processing in a service, you need to use a Thread or AsyncTask, so the processing does not occur on the UI thread.

Threads created on my service get killed when I close the activity

I created an activity that calls a service and the service creates a Thread that send and receive some data to/from the server, I can open other apps and the Service and the Thread run ok, but when I close the activity, the Service keeps running but the thread stops working. Why??? How can I keep the Thread running!!.
Code
Activity
package com.connectus.app;
public class ConnectUsActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connect_us);
Intent startServiceIntent = new Intent(getApplicationContext(), ConnectUsService.class);
startService(startServiceIntent);
}
Service
package com.connectus.app;
public class ConnectUsService extends Service {
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Thread t=new Thread(new Runnable() {
private DataInputStream in;
private BufferedReader br;
private DataOutputStream out;
#Override
public void run() {
Socket server=null;
try{
server=new Socket("10.10.40.58",4444);
in = new DataInputStream(server.getInputStream());
br=new BufferedReader(new InputStreamReader(in));
out = new DataOutputStream(server.getOutputStream());
while(true){
out.writeUTF("aaaaa");
String leido=in.readUTF();
out.writeUTF("asdf");
Thread.sleep(60000);
}
}catch(IOException ioe){
ioe.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
}
});
t.start();
return START_STICKY;
}
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
This is just a part of my code, I hope it helps.
Thanks everyone, finally i find a solution. It was necesary to use the setForeground() method, I just added this code to my service Class:
Notification note=new Notification();
startForeground(1337, note);
According to my research, this code is used to prevent that the service get killed by itself.
best regards!

Android Local Service Architecture

I'm trying to implement a service to handle the communication with the server for the following code. I don't know much about the design architecture for these.
Here is my service class
public class BgService extends Service {
private static final String TAG = BgService.class.getSimpleName();
private Timer timer;
SendJsonRequest sjr;
private TimerTask updateTask = new TimerTask(){
#Override
public void run(){
try{
SendJsonRequest sjr = new SendJsonRequest();
sjr.carMake();
Log.i(TAG, "LOOK AT ME");
}
catch(Exception e){
Log.w(TAG,e);
}
}
};
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate(){
super.onCreate();
Log.i(TAG, "Service creating");
timer = new Timer("Server listening timer");
timer.schedule(updateTask, 1000L, 60*1000L);
}
#Override
public void onDestroy(){
super.onDestroy();
Log.i(TAG, "Service Destroying");
timer.cancel();
timer = null;
}
}
Here is my SendJsonRequest class
public class SendJsonRequest{
private static final String TAG = "SendJsonRequest";
private static String URL = "xxxxxxxxx";
private static String infoRec;
public static void createJsonObj(String path, Map x){
infoRec = CreateJsonRequest.jsonRequest(URL+path, x );
System.out.println(infoRec);
}
public static void carMake(){
String path = "/CarMake";
Map<String, Object> z = new HashMap<String,Object>();
z.put("Name", "Ford");
z.put("Model", "Mustang");
createJsonObj(path, z);
}
}
Here is my CreateJsonObject class
public class CreateJsonRequest {
public static String jsonRequest(String URL, Map<String,Object> params){
try{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(URL);
JSONObject holder = new JSONObject();
for (Map.Entry<String, Object> m : params.entrySet()){
try {
holder.put(m.getKey(), m.getValue());
}
catch (JSONException e) {
Log.e("Hmmmm", "JSONException : "+e);
}
}
StringEntity se;
se = new StringEntity(holder.toString());
httpPost.setEntity(se);
httpPost.setHeader("Accept", "text/json");
httpPost.setHeader("Content-type", "text/json");
HttpResponse response = (HttpResponse) httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
if(entity != null){
InputStream is = entity.getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
String result= convertToString(is);
is.close();
System.out.println(result);
return result;
}
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
Sorry for the massive amount of code. How I implemented my service is obviously not correct, I just have no clue where to start to get a service handling the json requests to the server. Thanks in advance.
To be more clear, this did work on a button click, now I'm trying to get it to all run in the background with the service. So I guess my question is what goes where in the service?
My activity successfully starts the service, the service would work and print "look at me" to the logcat every minute. Then I added the try{ sjr.carMake()} and it catches an exception.
You can use a broadcast receiver. This is a way to have your code start at certain times indicated by Android OS - for example, you can have it start when Android finished booting up (this is where I run my services usually.
The best way is to use the AlarmManager class, and tell your service how often to run.
Tell us more about what you're trying to do, and what the problem is, and we can give you a more concise answer...
UPDATE:
Have you created an entry in the manifest.xml file for the service?
UPDATE
Here is how I'm doing it in my application. This is your "hook" to the OS. It's going to fire when it finishes booting (don't forget to make in entry in the manifest for this!)
public class TmBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(final Context context, final Intent bootintent) {
try{
Log.i("Taskmotion-ROBOT", "Robot Broadcast signal received on Boot. Trying to start Alarm scheduler");
Intent mServiceIntent = new Intent(context, ServiceAlarm.class);
context.startService(mServiceIntent);
}
catch(Exception e)
{
Log.i("Taskmotion", "Failed to start service...");
}
}
}
This Broadcast receiver calls a service that implements the AlarmManager class. The alarm manager sets up a schedule to run my service at a specified interval. Note that the alarms are deleted when the phone is shut down - but then recreated again when process is repeated as the phone boots back up and runs the BroadcastReceiver again.
public class ServiceAlarm extends Service {
private PendingIntent mAlarmSender;
#Override
public void onCreate() {
try{
Log.i("Taskmotion-ROBOT", "Setting Service Alarm Step 1");
mAlarmSender = PendingIntent.getService(this.getApplicationContext(),
0, new Intent(this.getApplicationContext(), BackgroundService.class), 0);
}
catch(Exception e)
{
Log.i("Taskmotion-ROBOT", "Problem at 1 :" + e.toString());
}
long firstTime = SystemClock.elapsedRealtime();
Log.i("Taskmotion-ROBOT", "Setting Service Alarm Step 2");
// Schedule the alarm!
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME,
firstTime, AlarmManager.INTERVAL_HOUR, mAlarmSender);
this.stopSelf();
}
#Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
I haven't refactored this code yet, it was my first go at it. I see now that I'm looking at it again that I could probably do the scheduling inside the BroadcastReceiver, but for the sake of getting you something that works, I'll continue.
As indicated by AlarmManager.INTERVAL_HOUR, my service will run once an hour. The service that I want to run is defined in the pendingIntent (BackgroundService.class). This is where you put your own service class.
I reworked your service class for you, and removed the timer (functionality replaced by the BroadcastReceiver & AlarmManager).
public class BgService extends Service {
private static final String TAG = BgService.class.getSimpleName();
SendJsonRequest sjr;
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate(){
super.onCreate();
Log.i(TAG, "Service creating");
//DO YOUR WORK WITH YOUR JSON CLASS HERE
//**************************************
//Make sure to call stopSelf() or your service will run in the background, chewing up
//battery life like rocky mountain oysters!
this.stopSelf();
}
#Override
public void onDestroy(){
super.onDestroy();
}
}

Why this thread is blocking my Android APP?

im making an Android app, i have a service, that update my local DB in background, by downloading data from the remote DB.
i have to put a thread on the service, because i dont know why, when i use a simple handle style bucle on the service it frozen my app during some secs when it is updating the local db. (i have my local db in a dbAdapter on MyApplication class)
OK, then i put a thread on the service, but i dont know why, if i start the service, the thread of the service is frezzing my APP :S. it's suposed that when u use services and threads code is executed in background and doesnt froze nothing, but in this case is frezzing my app. ¿how to avoid it?
this is the code of my service:
public class MyServiceLocalDB extends Service implements Runnable{
RemoteConnection con; //conexion remota
//para almacenar la config local de mi app
static SharedPreferences settings;
static SharedPreferences.Editor configEditor;
boolean serviceStopped;
private static MyDbAdapter mDb;
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
serviceStopped=false;
settings = PreferenceManager.getDefaultSharedPreferences(this.getApplicationContext());
configEditor = settings.edit();
con = new RemoteConnection();
mDb = new MyDbAdapter(this);
mDb.open();
}
#Override
public void onDestroy() {
//player.stop();
serviceStopped=true;
}
#Override
public void onStart(Intent intent, int startid) {
//player.start();
this.run();
}
public void updateDB()
{
mDb.clearDB();
List<Friend> myFriends=con.RetrieveFriends(settings.getString("login",""));
List<Permission> myPermissions=con.RetrievePermissions(settings.getString("login",""));
Permission p1 = null;
for (int i=0;i<myFriends.size();i++)
{
mDb.createUser(myFriends.get(i).getEmail(),myFriends.get(i).getFullName(),myFriends.get(i).getMovilephone(),myFriends.get(i).getMovileOperatingSystem(),myFriends.get(i).getPermission());
//p1=con.RetrievePermissionWithUser("pablo#upv.es", myFriends.get(i).getEmail());
}
for (int i=0;i<myPermissions.size();i++)
{
p1=myPermissions.get(i);
String hour1=formatHourFromTime(p1.getHour1());
String hour2=formatHourFromTime(p1.getHour2());
mDb.createPermission(p1.getFk_email1(),p1.getFk_email2(),""+p1.getValidated(),hour1,hour2,p1.getDate1Formated(),p1.getDate2Formated(),""+p1.getWeekend(),p1.getFk_type());
p1=null;
}
//MyApplication.getDatabaseAdapter().clearDB();
MyApplication.setDatabaseAdapter(mDb);
}
public String formatHourFromTime(Time time)
{
String hour1;
if (time.getHours()<10)
hour1="0"+time.getHours();
else
hour1=""+time.getHours();
if (time.getMinutes()<10)
hour1=hour1+":0"+time.getMinutes()+":00";
else
hour1=hour1+":"+time.getMinutes()+":00";
return hour1;
}
public void run() {
while (serviceStopped==false)
{
//handler.sendEmptyMessage(0);
try {
Thread.sleep(25000);// sleeps
} catch (InterruptedException e) {}
updateDB();
}
}
}
The onStart is called by the OS on a main UI thread, that's why you got stuck there (you block the main UI thread in the run()). Instead of this.run(); you should start a new Thread here - new Thread(this).start();.
BTW, onStart is deprecated. Implement onStartCommand instead.

Categories

Resources