1

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.

4

1 回答 1

0

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 :)

于 2012-05-21T22:40:25.490 回答