Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7962513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:21:26+00:00 2026-06-04T05:21:26+00:00

i wrote an application that listens received sms’. 2 problems occured. First , I

  • 0

i wrote an application that listens received sms’.
2 problems occured. First , I noticed that my service is running even if i close my app.
And second is that i couldn’t make it write to my sms.xml layout. It shows the Toast but not writing to the screen.

This is my activity.

import android.app.Activity;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.IBinder;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class SMSGoster extends Activity {

    public TextView t;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sms);
        t=(TextView) findViewById(R.id.smsText);
        t.setText("Okumaya Baslandı!\n");
    }

    public class SMSService extends Service {

        private SMSreceiver mSMSreceiver;
        private IntentFilter mIntentFilter;
        public TextView t;

        @Override
        public void onCreate() {
            super.onCreate();
            // SMS event receiver
            mSMSreceiver = new SMSreceiver();
            mIntentFilter = new IntentFilter();
            mIntentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
            registerReceiver(mSMSreceiver, mIntentFilter);
            t=(TextView) findViewById(R.id.smsText);
        }

        @Override
        public void onDestroy() {
            super.onDestroy();

            // Unregister the SMS receiver
            unregisterReceiver(mSMSreceiver);
        }

        @Override
        public IBinder onBind(Intent arg0) {
            // TODO Auto-generated method stub
            return null;
        }



    }

    private class SMSreceiver extends BroadcastReceiver {
        private final String TAG = this.getClass().getSimpleName();
        public TextView t;
        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle extras = intent.getExtras();

            String strMessage = "";

            if (extras != null) {
                Object[] smsextras = (Object[]) extras.get("pdus");

                for (int i = 0; i < smsextras.length; i++) {
                    SmsMessage smsmsg = SmsMessage
                            .createFromPdu((byte[]) smsextras[i]);

                    String strMsgBody = smsmsg.getMessageBody().toString();
                    String strMsgSrc = smsmsg.getOriginatingAddress();

                    strMessage += "SMS from " + strMsgSrc + " : " + strMsgBody;

                    Log.i(TAG, strMessage);
                    smsReceived(strMessage);
                    Toast.makeText(getApplicationContext(), strMessage,
                            Toast.LENGTH_LONG).show();
                }

            }

        }
        public void smsReceived(String s) {
            t=(TextView) findViewById(R.id.smsText);
            t.append(s);
        }
    }

}

This is the layout : sms.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/smsText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

And this is the manifes file :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.emre"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
     <activity
            android:name="SMSGoster"
            android:label="@string/app_name" >
            <service
                android:name="SMSService"
                android:enabled="true" >
                <intent-filter>
                    <action android:name="SMSService" />
                </intent-filter>
            </service>
        </activity>
    </application>
</manifest>

Any advice would be great!
Best Regards.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:21:27+00:00Added an answer on June 4, 2026 at 5:21 am

    I’ll answer one of your two questions:

    Services should be for things running in the background, for example, to monitor SMS messages (as I’m assuming you are)

    This means that it is active between activities.

    If you want to turn off the service when your app is closed, you have two options.

    • Kill the service on every Activity by overriding onPaused()
    • Create a “parent activity” that all of your activities inherit from that implements onPaused()

    I can post a code sample if you don’t know how to do these things 🙂

    SO encourages individual questions to be answered individually. So please write your 2nd question in a 2nd submission and I will go answer it as well. Include the link in a comment response!

    If you’re impatient, I will give you a hint: the service is very loosely coupled to the immediate UI, so you need to find a way to send something from a service to the UI… there is a method to do this 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a client that is running a custom VB 6 application they wrote
I need to write a .NET application that listens to a SMS message ,
I have wrote an application that syncs two folders together. The problem with the
I wrote a C# application that is a simple countdown timer. I use it
I have got a Wavecom Supreme GSM modem. I wrote a simple application that
I wrote an application in JAVA that adds articles to a Joomla site. My
I have a process that currently runs in a Delphi application that I wrote
I keep getting the error in VS 2100 CRT detected that the application wrote
I'm VERY new to Linq. I have an application I wrote that is in
I wrote a complex Java application with eclipse that uses many .jar libraries included

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.