This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyBroadcastReceiver extends BroadcastReceiver { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IntentFilter intentFilter = new IntentFilter(); | |
//創建一個IntentFilte物件 | |
intentFilter.addAction("Hello"); | |
//加入Action的辨識字串 | |
registerReceiver(new MyBroadcastReceiver() , intentFilter); | |
//註冊我們創建的BroadcastReceiver | |
Intent intent = new Intent(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyBroadcastReceiver extends BroadcastReceiver { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
if(intent.getAction().equals("Hello")){ | |
Log.d("我是MyBroadcastReceiver1" , "我聽到了喔"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unregisterReceiver(new MyBroadcastReceiver()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<receiver android:name=".MyBroadcastReceiver"> | |
<intent-filter> | |
<action android:name="Hello" /> | |
</intent-filter> | |
</receiver> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BootBroadcastReceiver extends BroadcastReceiver { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){ | |
//Intent.ACTION_BOOT_COMPLETED == android.intent.action.BOOT_COMPLETED | |
Intent intent1 = new Intent(context , MainActivity.class); | |
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
context.startActivity(intent1); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<receiver android:name=".BootBroadcastReceiver"> | |
<intent-filter> | |
<action android:name="android.intent.action.BOOT_COMPLETED"/> | |
</intent-filter> | |
</receiver> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void sendNotification() { | |
int notificationId = 001; | |
//決定這個notification的辨識編號 | |
String[] replyLabels = {"Apple" , "Banana" , "Cat" , "Dog" , "Frog"}; | |
//給使用者的預設選項 最多五個 | |
RemoteInput remoteInput = new RemoteInput.Builder("extra_voice_reply") | |
.setLabel("請說...") | |
.setChoices(replyLabels) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private CharSequence getMessageText(Intent intent) { | |
Bundle remoteInput = RemoteInput.getResultsFromIntent(intent); | |
if (remoteInput != null) { | |
return remoteInput.getCharSequence("extra_voice_reply"); | |
} | |
return null; | |
} |