如果系统日期在android中自动更改,则触发接收器

时间:2015-10-20 07:45:48

标签: android broadcastreceiver

我是android的新手。我使用android.intent.action.DATE_CHANGED在清单中注册了接收器,当用户手动更改日期时,它正常工作。但我的要求是接收器必须每天自动调用系统日期更改说在凌晨12:00。用户不应更改日期。

提前致谢。

我的代码 - 清单文件

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".FirstActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".NotificationOne"
        android:label="@string/title_activity_notification_one"
        android:parentActivityName=".FirstActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".FirstActivity" />
    </activity>

    <service android:name=".MyService">
    </service>

    <receiver android:name=".MyBroadcastreceiver" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>

    <receiver android:name=".SecondBroadcastReceiver">
        <intent-filter>
            <action android:name="android.intent.action.DATE_CHANGED"/>
        </intent-filter>
    </receiver>
</application>

广播接收器类

public class SecondBroadcastReceiver extends BroadcastReceiver {
private Context mContext;
@Override
public void onReceive(Context context, Intent intent) {
    mContext = context;
    Toast.makeText(context,"inside second",Toast.LENGTH_SHORT).show();
    MyBroadcastreceiver.firstConnect = true;
    MyBroadcastreceiver.hasInternet = isInternetAvailable();
    if(MyBroadcastreceiver.hasInternet) {
        context.startService(new Intent(context, MyService.class));
        MyBroadcastreceiver.firstConnect = false;
    }
}

public boolean isInternetAvailable() {

    Toast.makeText(mContext,"inside isInternetAvailable",Toast.LENGTH_SHORT).show();
    ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

}

一项服务 - 有通知

public class MyService extends Service {

private NotificationManager myNotificationManager;
private int notificationIdOne = 111;
private int numMessagesOne = 0;
@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();

        displayNotificationOne();
    return START_STICKY;
}

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

我有另一个接收器,当互联网连接/断开时将被呼叫。

0 个答案:

没有答案