Run Application in background


Last Updated:

  1. Ganesh

    Ganesh Member This Topic's Starter

    Joined:
    Dec 25, 2009
    Messages:
    18
    Likes Received:
    0
    Hello Friends,

    I want to develope an application in which if SIM card is changed then the application should sent SMS to a predefined number from the new SIM. User should not know that SMS are being send from his/ her mobile. For this application needs to run in background. How can I achieve it?


    Thank you in advance
     

    Advertisement
  2. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    Hi Ganesh,

    To run something in the background you'll need to write a Service.
    I don't have any experience with them myself, so all I can do is point you at some documentation.

    Service | Android Developers

    Regards,

    Mark
     
  3. Ganesh

    Ganesh Member This Topic's Starter

    Joined:
    Dec 25, 2009
    Messages:
    18
    Likes Received:
    0
    Hello Mark,

    Thank you for your reply. I tried to use the link given by you, but unfortunately the link seens to be broken. Any other sugesstion is welcome :)



    Thank you
     
  4. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    I just clicked on the link and it worked for me.

    The URL is

    developer.android.com/reference/android/app/Service.html

    Or just go to the Android developer reference site and search for service.
    Or google for android service.

    Mark
     
  5. Ganesh

    Ganesh Member This Topic's Starter

    Joined:
    Dec 25, 2009
    Messages:
    18
    Likes Received:
    0
    Hello Mark,

    I tried with the use of Service. but it does not worked

    My Activity class is as follows










    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.animation.Animation;
    import android.view.animation.AnimationUtils;
    import android.widget.Button;
    import android.widget.LinearLayout;
    public class MSMAndroid extends Activity {
    private static final String TAG = "MSMAndroid";
    /** Called when the activity is first created. */
    Button btn_StartMSM;
    LinearLayout layout;
    LinearLayout layMain;

    @Override

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    btn_StartMSM =new Button(this);
    btn_StartMSM.setText("Button");
    layout = new LinearLayout(this);
    layMain= new LinearLayout(this);
    layout.addView(btn_StartMSM);
    setContentView(layout);


    btn_StartMSM.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

    Intent int1 = new Intent( );
    startService(int1 );

    //Intent int = new Intent( this, MyService.class );
    //startService(int );
    //Intent a1 =new Intent(MSMAndroid.this,MSMService.class);
    //startService(a1);
    //Animation anim = AnimationUtils.loadAnimation(MSMAndroid.this, 1);
    //MSMAndroid.this.layMain.startAnimation(anim);
    //startService();
    }
    });
    }
    public void startService ()
    {
    Intent serviceIntent = new Intent(this, MSMService.class);
    Log.d(TAG, "Start service");
    startService(serviceIntent);
    btn_StartMSM.setText("After Start");
    }


    }









    My service class is

    package com.MSMAndroid;
    import android.app.PendingIntent;
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    import android.telephony.gsm.SmsManager;
    import android.widget.LinearLayout;
    public class MSMService extends Service{
    MSMAndroid msmAndroid;
    SmsManager smsManager;
    PendingIntent i1;
    LinearLayout layout2;
    String destAddr = "919860650337", mMessageText =
    "Welcome to MSMAndroid";
    @Override
    public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
    }
    @Override
    public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    msmAndroid = new MSMAndroid();
    smsManager =SmsManager.getDefault();
    layout2 = new LinearLayout(this);
    i1 = PendingIntent.getBroadcast(this, 0, new Intent(), 0);
    //destAddr=""+phonenumber.getText();
    smsManager.sendTextMessage(destAddr, null, mMessageText, i1, null);
    msmAndroid.btn_StartMSM.setText("HERE");
    msmAndroid.setContentView(msmAndroid.layout);
    //msmAndroid.setContentView(layout2);
    //setForeground(true);



    }
    public void onStart(final Intent intent, final int startId) {
    super.onStart(intent, startId);

    }


    }


    My AndroidMenifest is


    <?​
    xml version="1.0" encoding="utf-8"?>
    <
    manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.MSMAndroid"

    android:versionCode="1"

    android:versionName="1.0">

    <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">

    <activity android:name=".MSMAndroid"

    android:label="@string/app_name">

    <service android:name=".MSMService"/>

    <intent-filter>

    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />

    </intent-filter>

    </activity>


    </application>

    <uses-sdk android:minSdkVersion="2" />
    <
    uses-permission android:name="android.permission.SEND_SMS"></uses-permission>

    </
    manifest>

    My R.java is as follows


    /* AUTO-GENERATED FILE. DO NOT MODIFY.
    *
    * This class was automatically generated by the
    * aapt tool from the resource data it found. It
    * should not be modified by hand.
    */
    package com.MSMAndroid;
    public final class R {
    public static final class attr {
    }
    public static final class drawable {
    public static final int icon=0x7f020000;
    }
    public static final class layout {
    public static final int main=0x7f030000;
    }
    public static final class string {
    public static final int app_name=0x7f040001;
    public static final int hello=0x7f040000;
    }
    }



    I am not getting where I am commiting the mistake


    Thank you in advance
     
  6. KeithG

    KeithG Well-Known Member

    Joined:
    Nov 15, 2009
    Messages:
    103
    Likes Received:
    3
    You shouldn't try instantiate an activity via the service with the default new constructor.

    You need to send an intent up to the VM to ask it to start the activity.
     
  7. Ganesh

    Ganesh Member This Topic's Starter

    Joined:
    Dec 25, 2009
    Messages:
    18
    Likes Received:
    0
    Hello KeithG,


    I did not get what you are trying to say. Actually what logic I am trying to implement is as follow...

    1. Start Activity ( this will show a button on screen so that user can see this button for his interaction)
    2. After click on button Service should (which will send SMS constantly) start.
    3. As service is supposed to run in background; I am expecting that the user should see his normal desktop background (not our application Screen with Button)

    Thank you in advance
     

Share This Page

Loading...