IT/Android

Receiver

바바옄 2015. 6. 23. 10:31
반응형

1. 문자메세지가 오면 문자를 받아서 Toast 해주는 Application

 

MySMSReceiver.java

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.ktds.mysmsreceiver;
 
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
 
import java.util.Date;
 
public class MySMSReceiver extends BroadcastReceiver {
    public MySMSReceiver() {
    }
 
    @Override
    public void onReceive(Context context, Intent intent) {
 
        if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
 
            Bundle bundle = intent.getExtras();
            // pdus : 문자로 받은 정보를 가져옴
            Object[] messages = (Object[])bundle.get("pdus");
            SmsMessage smsMessage[] = new SmsMessage[messages.length];
 
            Date currentDate = null;
            String incommingNumber = "", message="", formattedMessage="";
 
            for(int i = 0; i < messages.length; i++){
                smsMessage[i] = SmsMessage.createFromPdu((byte[]) messages[i]);
 
                //수신 시간 확인
                currentDate = new Date(smsMessage[i].getTimestampMillis());
 
                // 발신 번호 확인
                incommingNumber = smsMessage[i].getOriginatingAddress();
 
                // 메세지 확인
                message = smsMessage[i].getMessageBody().toString();
 
                formattedMessage = String.format("%s, %s, %s", currentDate.toString(), incommingNumber, message);
                Toast.makeText(context,formattedMessage, Toast.LENGTH_LONG).show();
            }
        }
    }
}
 
cs

 

 

AndroidManufest.xml

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ktds.mysmsreceiver" >
 
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
 
        <receiver
            android:name=".MySMSReceiver"
            android:enabled="true"
            android:exported="true" >
 
            <intent-filter>
                    <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
            </intent-filter>
 
        </receiver>
    </application>
 
</manifest>
 
cs

 

 

 

 

 

 

 

2. 문자메세지가 오면 새로운 Activity 띄어서 문자 내용 출력하기

 

MainActivity.java

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.ktds.mysmsreceiver;
 
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
 
 
public class MainActivity extends ActionBarActivity {
 
    private TextView subject;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        subject = (TextView) findViewById(R.id.subject);
        Intent intent = getIntent();
        String sms = intent.getStringExtra("SMS");
 
        subject.setText(sms);
 
    }
 
    @Override
    protected void onNewIntent(Intent intent) {
        String sms = intent.getStringExtra("SMS");
        subject.setText(sms);    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
 
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
 
        return super.onOptionsItemSelected(item);
    }
}
 
cs

 

 

MySMSReceiver.java

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.ktds.mysmsreceiver;
 
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
 
import java.util.Date;
 
public class MySMSReceiver extends BroadcastReceiver {
    public MySMSReceiver() {
    }
 
    @Override
    public void onReceive(Context context, Intent intent) {
 
        String sms = getSMSDescription(intent);
        if(sms != null){
            Intent activityIntent = new Intent();
            activityIntent.setClass(context, MainActivity.class);
            activityIntent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK | intent.FLAG_ACTIVITY_CLEAR_TOP | intent.FLAG_ACTIVITY_SINGLE_TOP);
 
            activityIntent.putExtra("SMS",sms);
            context.startActivity(activityIntent);
        }
    }
 
    private String getSMSDescription(Intent intent){
 
        if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
 
            Bundle bundle = intent.getExtras();
            // pdus : 문자로 받은 정보를 가져옴
            Object[] messages = (Object[])bundle.get("pdus");
            SmsMessage smsMessage[] = new SmsMessage[messages.length];
 
            Date currentDate = null;
            String incommingNumber = "", message="", formattedMessage="";
 
            for(int i = 0; i < messages.length; i++){
                smsMessage[i] = SmsMessage.createFromPdu((byte[]) messages[i]);
 
                //수신 시간 확인
                currentDate = new Date(smsMessage[i].getTimestampMillis());
 
                // 발신 번호 확인
                incommingNumber = smsMessage[i].getOriginatingAddress();
 
                // 메세지 확인
                message = smsMessage[i].getMessageBody().toString();
 
                formattedMessage += String.format("%s, %s, %s \n", currentDate.toString(), incommingNumber, message);
 
            }
            return formattedMessage;
        }
        return  null;
    }
 
 
    /*   @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
            Bundle bundle = intent.getExtras();
            // pdus : 문자로 받은 정보를 가져옴
            Object[] messages = (Object[])bundle.get("pdus");
            SmsMessage smsMessage[] = new SmsMessage[messages.length];
            Date currentDate = null;
            String incommingNumber = "", message="", formattedMessage="";
            for(int i = 0; i < messages.length; i++){
                smsMessage[i] = SmsMessage.createFromPdu((byte[]) messages[i]);
                //수신 시간 확인
                currentDate = new Date(smsMessage[i].getTimestampMillis());
                // 발신 번호 확인
                incommingNumber = smsMessage[i].getOriginatingAddress();
                // 메세지 확인
                message = smsMessage[i].getMessageBody().toString();
                formattedMessage = String.format("%s, %s, %s", currentDate.toString(), incommingNumber, message);
                Toast.makeText(context,formattedMessage, Toast.LENGTH_LONG).show();
            }
        }
    }*/
}
 
cs

 

 

AndroidManufest.xml

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ktds.mysmsreceiver" >
 
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
 
        <receiver
            android:name=".MySMSReceiver"
            android:enabled="true"
            android:exported="true" >
 
            <intent-filter>
                    <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
            </intent-filter>
 
        </receiver>
    </application>
 
</manifest>
 
cs

 

 

activity_main.xml

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
 
    <TextView
        android:id="@+id/subject"
        android:layout_centerInParent="true"
        android:textSize="40dp"
        android:textStyle="bold"
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 
</RelativeLayout>
 
cs

 

 

 

 

 

 

 

 

 

반응형