2015-11-05 88 views
0

我被卡住了,看起來很容易。我想創建一個包含兩個按鈕的簡單應用程序:一個用於啓動服務,另一個用於停止它。我已經建立了我NotifyService類:短信通知 - 不能正常工作

public class NotifyService extends Service { 
    public NotifyService() { 
    } 

    private static final String SMS_RECEIVED="android.provider.Telephony.SMS_RECEIVED"; 

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      displayNotification(intent); 
     } 
    }; 

    private void displayNotification(Intent intent) 
    { 
     if(intent.getAction().equals(SMS_RECEIVED)) { 
      int NOTIFICATION=R.string.local_service_started; 

      //notification creating 
      Notification.Builder notificationBuilder = new Notification.Builder(this) 
        .setContentText("Otrzymano smsa!") 
        .setContentTitle("SMS!") 
        .setSmallIcon(android.R.drawable.btn_plus); 
      Notification note = notificationBuilder.build(); 

      //getting system service 
      NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

      //displaying notification 
      notificationManager.notify(NOTIFICATION, note); 
     } 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     unregisterReceiver(broadcastReceiver); 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     registerReceiver(broadcastReceiver,new IntentFilter(SMS_RECEIVED)); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

下面是我的MainActivity代碼:每次我模擬時間

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.pablo.myapplication" > 

    <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" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <service 
      android:name=".NotifyService" 
      android:enabled="true" 
      android:exported="true" > 
     </service> 
    </application> 

不幸的是,:

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    public void startServiceBtn(View view) 
    { 
     Intent intent = new Intent(this,NotifyService.class); 
     startService(intent); 
    } 

    public void stopServiceBtn(View view) 
    { 
     Intent intent = new Intent(this,NotifyService.class); 
     stopService(intent); 
    } 

而且清單通過Android Device Monitor發送短信,它不起作用,它向我顯示了默認的系統通知(即使沒有清單中的權限也會顯示該通知) IST是正確的行爲)

編輯:Android Device Monitor它仍保持舒Permission Denial: ... requires android.permission.RECEIVE_SMS due to sender.com android...。然而,我已經將其添加到意向過濾器中,然後我不知道它爲什麼會發生。

回答

0

這個問題的答案與其他一些與我有上次權限有關的問題有關,它與Marshmallow的新權限政策有關。更多信息here

所以這個問題可以通過切換到較低的sdk版本或在運行時調用appriopriate方法(查看上面的鏈接)來解決。