如何在清单中的服务中声明接收者?

时间:2016-03-21 10:57:11

标签: java android

我在服务中有一个接收器。我如何在清单中声明接收器?我需要接收器,因为我有预定的闹钟。我如何在服务中声明接收器?代码如下。逻辑/语法是否正确?接收器中的代码只执行一次。为什么?可能是什么问题

package com.todaysfuture.dynpin;


/**
* Created by rishabh on 14/2/16.
*/
public class MyService  extends Service {

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    //does some stuff


    final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            {
                //does some stuff
            }
        }
    };
    registerReceiver(receiver, filter);

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

}

1 个答案:

答案 0 :(得分:0)

我将在这里举一个你问的例子 例如,开启/关闭屏幕。
希望能帮到你。

服务:

public class MyService extends Service {
    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            boolean res;
            if (action.equals("android.intent.action.SCREEN_ON")) {
                res = true;
            } else if (action.equals("android.intent.action.SCREEN_OFF")) {
                res = true;
            }
        }
    };

    @Override
    public void onCreate() {
        Toast.makeText(this, "The new Service was Created", Toast.LENGTH_LONG).show();
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.intent.action.SCREEN_ON");
        filter.addAction("android.intent.action.SCREEN_OFF");

        registerReceiver(receiver, filter);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        // For time consuming an long tasks you can launch a new thread here...
        Toast.makeText(this, " Service Started", Toast.LENGTH_LONG).show();

    }

    @Override
    public void onDestroy() {
        Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
        unregisterReceiver(receiver);
    }

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

启动服务的活动:

public class MainActivity extends AppCompatActivity {

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

        startService(new Intent(this, MyService.class));
    }
}

清单:

<manifest package="com.test.myapplication"
          xmlns:android="http://schemas.android.com/apk/res/android">

    <action android:name="android.intent.action.SCREEN_OFF"></action>
    <action android:name="android.intent.action.SCREEN_ON"></action>

    <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=".MyService"
            android:enabled="true"
            android:exported="true" >
        </service>
    </application>

</manifest>