Android Forums

Go Back   These FORUMS ARE DISABLED!! OUR NEW FORUMS ARE AT http://androidforum.com/ > Android Coders > Android Development, Answers, Tutorials, and Code Snippets
Connect with Facebook

Click Here To Register!
 
 
LinkBack Thread Tools Display Modes
Old 02-01-2010, 03:51 AM   #1
Junior Member
 
Join Date: Jan 2010
Posts: 2
Friends: 0
View pramod.deore's Profile   View pramod.deore's Photo Album   View pramod.deore's Blog   View Social Groups
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
Code:
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
Code:
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
Code:
<?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
pramod.deore is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
 

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
Fixing background littlebird Android Chat 1 02-14-2010 08:46 AM
Impossible to reactivate background data Léobaillard Android Development, Answers, Tutorials, and Code Snippets 1 12-14-2009 05:28 AM
Background lighting tom c Droid 3 12-07-2009 10:36 PM
Does Skype run in the background and look for calls on Android gnagy Android Games And Applications 1 09-19-2009 12:10 PM
Background in a view Wuque Android Development, Answers, Tutorials, and Code Snippets 1 06-09-2009 01:49 PM


Unlocked G1 Phones | Buy T-Mobile G1 | Google Phone

All times are GMT -6. The time now is 05:03 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.
Copyright (c) 2012 TalkAndroid.com. All rights reserved.