我使用android studio进行编程,我希望每次时钟更改时(这意味着每分钟,例如电话时间从3:13更改为3:14),都需要广播接收器来执行某项操作,即使该程序没有运行。所以我应该使用带Time_Tick动作的广播接收器。但是它仅在活动创建时起作用。任何想法?

主要活动:

public class MainActivity extends ActionBarActivity {

    BroadcastReceiver br;
    final SmsManager sms = SmsManager.getDefault();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        IntentFilter i=new IntentFilter();
        i.addAction("android.intent.action.TIME_TICK");
        registerReceiver(new MyReceiver(),i);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}


MyReceiver:

public class MyReceiver extends BroadcastReceiver {
    public MyReceiver() {
        Log.i("Saaalam","saaaalaalam");
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}


表现:

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

    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="ANDROID.PERMISSION.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true" >
            <intent-filter> <action android:name="android.intent.action.TIME_TICK"></action> </intent-filter>
        </receiver>
    </application>

</manifest>

最佳答案

这是时间刻度的示例代码。 TIME_TICK意图被更改为分钟更改。

静态TimeReceiver tickReceiver = new TimeReceiver();

void setTimeTick()
{
     registerReceiver(tickReceiver, new IntentFilter(
                    "android.intent.action.TIME_TICK"));
     Toast.makeText(this, "Registered broadcast receiver", Toast.LENGTH_SHORT)
                    .show();

     //Register the broadcast receiver to receive TIME_TICK
     registerReceiver(tickReceiver, new IntentFilter(Intent.ACTION_TIME_TICK));
}


收信人阶层

class TimeReceiver extends BroadcastReceiver {
    @Override
public void onReceive(Context context, Intent arg1) {
        // TODO Auto-generated method stub
        Toast.makeText(context, "Time tick", Toast.LENGTH_LONG).show();
    }
}

10-08 20:13