簡體   English   中英

Android的廣播接收器不起作用

[英]android Broadcast Receiver not works

我使用android studio進行編程,我希望每次時鍾更改時(例如,電話時間從3:13更改為3:14每一分鍾),都需要廣播接收器來執行某項操作,即使該程序沒有運行。 所以我應該將廣播接收器與Time_Tick Action配合使用。 但是它僅在活動創建時起作用。 任何想法?

主要活動:

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

您可以將此代碼添加到清單中嗎

<intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

據我了解,您需要在一定時間(例如一分鍾)后執行某些任務。 為此,您可以使用AlarmManager

AlarmManager myAlarmManager = Context.getSystemService(Context.ALARM_SERVICE)

AlarmManager的示例: https ://androidresearch.wordpress.com/2012/07/02/scheduling-an-application-to-start-later/

或Android在Android 5.0(API 21)中引入了JobScheduler API。 最好使用AlarmManager,因為它不會考慮設備是連接到電源插頭,閑置還是已連接。

JobScheduler示例: 如何使用 Androids Job Scheduler

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM