Hello everybody, In my application I want to send sms, but the
application must have to run in the background. It send the sms but it
is not run in the background. I have following code.

//Background.java

package com.micro;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class Background extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        startService(new Intent(this, MyService.class));
    }
}

##########################################
My Service class look like this

//MyService.java

package com.micro;

import android.app.Activity;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.telephony.gsm.SmsManager;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service
{
                @Override
                public IBinder onBind(Intent intent)
                {
                        return null;
                }

                @Override
                public void onCreate()
                {
                        sendSMS("9960510915","Pramod Deore");

                }

                @Override
                public void onDestroy()
                {

                }

                @Override
                public void onStart(Intent intent, int startid)
                {

                }

                 public  void sendSMS(String phoneNumber, String message)
                    {
                        String SENT = "SMS_SENT";
                        String DELIVERED = "SMS_DELIVERED";

                        PendingIntent sentPI = PendingIntent.getBroadcast(this,
0,new Intent(SENT), 0);

                        PendingIntent deliveredPI = 
PendingIntent.getBroadcast(this,
0,new Intent(DELIVERED), 0);

                        //---when the SMS has been sent---
                        registerReceiver(new BroadcastReceiver(){

                            public void onReceive(Context arg0, Intent arg1)
                            {
                                switch (getResultCode())
                                {
                                    case Activity.RESULT_OK:
                                        Toast.makeText(getBaseContext(), "SMS
sent",
                                                Toast.LENGTH_SHORT).show();
                                        break;
                                    case 
SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                                        Toast.makeText(getBaseContext(), 
"Generic
failure",
                                                Toast.LENGTH_SHORT).show();
                                        break;
                                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                                        Toast.makeText(getBaseContext(), "No
service",
                                                Toast.LENGTH_SHORT).show();
                                        break;
                                    case SmsManager.RESULT_ERROR_NULL_PDU:
                                        Toast.makeText(getBaseContext(), "Null
PDU",
                                                Toast.LENGTH_SHORT).show();
                                        break;
                                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                                        Toast.makeText(getBaseContext(), "Radio
off",
                                                Toast.LENGTH_SHORT).show();
                                        break;
                                }
                            }
                        }, new IntentFilter(SENT));

                        //---when the SMS has been delivered---
                        registerReceiver(new BroadcastReceiver()
                        {
                                public void onReceive(Context arg0, Intent arg1)
                                {
                                        switch (getResultCode())
                                        {
                                        case Activity.RESULT_OK:
                                        Toast.makeText(getBaseContext(), "SMS
delivered",Toast.LENGTH_SHORT).show();
                                        break;
                                        case Activity.RESULT_CANCELED:
                                        Toast.makeText(getBaseContext(), "SMS 
not
delivered",Toast.LENGTH_SHORT).show();
                                        break;
                                        }
                                }
                        },new IntentFilter(DELIVERED));

                        SmsManager sms = SmsManager.getDefault();
                        sms.sendTextMessage(phoneNumber, null, message, sentPI,
deliveredPI);
                    }


        }


##########################################

//AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android";
      package="com.micro"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/
app_name">
        <activity android:name=".Background"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category
android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
<service android:enabled="true" android:name=".MyService" />

    </application>
    <uses-sdk android:minSdkVersion="3" />

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

    <uses-permission android:name="android.permission.RECEIVE_SMS">
    </uses-permission>

</manifest>

##########################################

But when I run it It is not running in the background.

Thanks

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to