-1
對於在ANDROID STUDIO中接收短信的Toast演示程序,我沒有得到任何錯誤...但它不適用於我的任何手機(使用三星GT-i9082和聯想a6000 + )。這裏是我的代碼和清單以下分別......Toast on SMS接收ANDROID STUDIO無法正常工作
package com.myapplication.siva.phoneapp;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity{
private BroadcastReceiver receiver;
public static final String SMS_BUNDLE = "pdus";
private static String TAG="com.myapplication.siva.phoneapp";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG,"Inside onCreate");
receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle intentExtras = intent.getExtras();
Log.i(TAG,"Inside on Receive");
try {
Toast.makeText(context, "Coming Inside",Toast.LENGTH_SHORT).show();
Log.i(TAG,"Inside try");
if (intentExtras != null) {
Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
String smsMessageStr = "";
for (int i = 0; i < sms.length; ++i) {
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);
String smsBody = smsMessage.getMessageBody().toString();
String address = smsMessage.getOriginatingAddress();
smsMessageStr += "SMS From: " + address + "\n";
smsMessageStr += smsBody + "\n";
}
Toast.makeText(context, smsMessageStr, Toast.LENGTH_SHORT).show();
}
}catch (Exception e)
{
Log.i(TAG,"Some Exception");
}
}
};
}
@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);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapplication.siva.phoneapp" >
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS"></uses-permission>
<uses-permission android:name="android.permission.hardware_test"></uses-permission>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<receiver
android:name=".MainActivity"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"> </action>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>
</activity>
</application>
</manifest>
那麼,一個問題是,你的''的'<意圖過濾器>'應該有' 「android.provider.Telephony.SMS_RECEIVED」'的'',不'「com.myapplication.siva。所有MyApplication「'。此外,由於Android 3.1,你需要包括用戶可以至少運行一次,以使應用程序在其_stopped_狀態的活動。您的廣播接收器將無法工作,直到然後。 –
我更新了給corrections..Even雖然它不是working..The計劃上面是我更新了一個... – Siva