0

我已经添加了AndroidManifest.xml,我认为日志的标签“CWCGR1”会每分钟记录一次。但没有发生,似乎onReceive()没有运行,我的应用程序有什么问题?谢谢!

AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.enabledisablebroadcastreceiver"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="15" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.code4reference.enabledisablebroadcastreceiver.EnableDisableBroadcastReceiver"
            android:label="@string/title_activity_enable_disable_boradcast_receiver" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- Broadcast receiver -->
        <receiver android:name="com.code4reference.enabledisablebroadcastreceiver.AlarmManagerBroadcastReceiver" >
            <intent-filter>
               <action android:name="android.intent.action.TIME_TICK" >
               </action>
            </intent-filter>
        </receiver>
    </application>

</manifest>

EnableDisableBroadcastReceiver.java:

package com.code4reference.enabledisablebroadcastreceiver;
import com.example.enabledisablebroadcastreceiver.R;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class EnableDisableBroadcastReceiver extends Activity {

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

   public void enableBroadcastReceiver(View view){
       ComponentName receiver = new ComponentName(this, AlarmManagerBroadcastReceiver.class);
       PackageManager pm = this.getPackageManager();

       pm.setComponentEnabledSetting(receiver,
               PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
               PackageManager.DONT_KILL_APP);
       Toast.makeText(this, "Enabled broadcast receiver", Toast.LENGTH_SHORT).show();
   }

   public void disableBroadcastReceiver(View view){
       ComponentName receiver = new ComponentName(this, AlarmManagerBroadcastReceiver.class);
       PackageManager pm = this.getPackageManager();

       pm.setComponentEnabledSetting(receiver,
               PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
               PackageManager.DONT_KILL_APP);
       Toast.makeText(this, "Disabled broadcst receiver", Toast.LENGTH_SHORT).show();
   }     

}

AlarmManagerBroadcastReceiver.java:

package com.code4reference.enabledisablebroadcastreceiver;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.util.Log;
import android.widget.Toast;

public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {

    final public static String ONE_TIME = "onetime";
    @Override
    public void onReceive(Context context, Intent intent) {

         //You can do the processing here update the widget/remote views.
         StringBuilder msgStr = new StringBuilder();

         Format formatter = new SimpleDateFormat("hh:mm:ss a");
         msgStr.append(formatter.format(new Date()));

         Toast.makeText(context, msgStr, Toast.LENGTH_SHORT).show();

         Log.d("CWCGR1",msgStr.toString());
    }
}
4

1 回答 1

1

您必须从您的活动中调用 sendBroadcast :

Intent broadcast = new Intent();
broadcast.setAction("android.intent.action.TIME_TICK");
sendBroadcast(broadcast);

您可以根据您的意图设置参数。

于 2013-10-12T06:45:23.243 回答