短信通知 - 不工作 [英] sms notification- not working

查看:57
本文介绍了短信通知 - 不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被一些看似简单的事情困住了.我想创建一个简单的应用程序,其中包含两个按钮:一个用于启动服务,另一个用于停止服务.我已经创建了我的 NotifyService 类:

I'm stuck with something that seems easy. I want to create a simple app that contains of two buttons: one to start a service and a second one to stop it. I've created my NotifyService class:

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 的代码:

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);
    }

还有清单:

<?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>

不幸的是,每次我通过 Android Device Monitor 模拟发送短信时,它不起作用,它向我显示默认系统通知(顺便说一下,即使没有清单中的许可是正确的行为吗?)

Unfortunately, every time I'm simulating a sending of an sms through Android Device Monitor, it doesn't work, it shows me the default system notification (that by the way is shown even without the permission in manifest- ist that right behavior?)

Android Device Monitor 中,它仍然显示 Permission Denial: ... 由于 sender.com android... 需要 android.permission.RECEIVE_SMS....然而,我已将其添加到意图过滤器中,然后我不知道为什么会发生这种情况.

In Android Device Monitor it still keeps showin Permission Denial: ... requires android.permission.RECEIVE_SMS due to sender.com android.... Yet, I've added this into intent filter, then I don't know why it's happening.

推荐答案

这个问题的答案与我上次遇到的一些其他有关权限的问题有关,并且与 Marshmallow 的新权限策略有关.更多信息此处.

The answer to this question was related to some other matters with permissions that I had last times and it's connected with new permission politics with Marshmallow. More info here.

因此可以通过切换到较低的 sdk 版本或在运行时调用适当的方法(查看上面的链接)来解决问题.

So the problem can be solver by switching to lower sdk version or calling appriopriate methods (look into above link) in runtime.

这篇关于短信通知 - 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆