2015-11-16 41 views
-2

我正在製作應用程序,當手機收到帶有特定內容(如密碼)的短信時應啓動該應用程序。我已經嘗試了很多代碼,但它仍然無法工作。任何人都可以請幫我嗎?如何讓應用程序在您收到短信時運行

這是我的代碼: SmsReceiver.java

package com.example.william.better_gps; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.widget.Toast; 

public class SmsReceiver extends BroadcastReceiver { 

public void onReceive(Context context, Intent intent) { 

    Toast.makeText(context, "message", Toast.LENGTH_LONG).show(); 

} 
} 

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.william.better_gps" > 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.READ_SMS" /> 
<uses-permission android:name="android.permission.RECEIVE_SMS" /> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme.NoActionBar" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <receiver android:name=".SmsReceiver" 
     android:enabled="true"> 
     <intent-filter android:priority="999"> 
      <action android:name="android.provider.Telephony.SMS_RECEIVED"/> 

     </intent-filter> 
    </receiver> 



</application> 
</manifest> 

哦,是有什麼辦法,我可以模擬一個短信,所以我不必問別人發短信給我,如果我想檢查它是否正常工作

+0

你是什麼buildToolsVersion,compileSdkVersion和targetSdkVersion? –

+0

你是否檢查過該設備的android api版本?你注意到只有api 19或更高版本纔有意圖嗎? – Christian

+0

@Ashraful伊斯蘭我buildToolsVersion是23.0.2,我的compileSdkVersion是23,我的targetSdkVersion也是23. –

回答

1

不要在廣播接收機SMS_RECEIVED API級別23的動作用compileSdkVersion,targetSdkVersion,buildToolsVersion 23或大於that.Because給予許可被拒絕
使用21或低於that.below配置對我的作品

compileSdkVersion 21
buildToolsVersion 「21.1.2」
targetSdkVersion 21

+0

如果你建立它與23沒有設備將調用SMS_RECEIVED廣播接收器生成21或更低它將適用於所有設備,我已經測試與我的nexus 5其中有Android棉花糖(API 23)和工作 –

+0

您的應用程序的目標API級別是23,即android M(6.0)。在Android M中有相關的用戶權限的巨大變化。檢查答案http://stackoverflow.com/questions/33036523/broadcast-receivers-not-working-in-android-6-0-marshmallow –

+0

有一件事,我正在回答你的問題,所以如果你應用它,但它不起作用,那麼你應該對它進行評論並要求我證明 –

1

您正在設置應用程序作爲啓動程序運行,只有當您點擊圖標才能運行 跑。您需要在接收短信時指定啓動應用程序的意圖過濾器。然後在代碼中,您可以在主要活動中設置一個廣播接收器來偵聽後續文本。

<activity 
    android:name=".MainActivity" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme.NoActionBar" > 
    <intent-filter> 
     <action android:name="android.provider.Telephony.SMS_RECEIVED"/> 
     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
</activity> 
+0

但是如果我這樣做,我不能再在我的設備上運行它,因爲我得到一個錯誤「MainActivity不能分配給android.app.Activity」。我非常新的android編程,所以也許它有另一個原因,我不能運行它 –

相關問題