[英]Pass Data from an activity to launcher activity
我想將數據從活動傳遞到啟動器活動,而無需實際打開發送者活動, 即
在不訪問發送方活動的情況下,我希望在打開應用程序后立即將一些數據傳遞到啟動程序活動中 。
可能嗎??
謝謝。
如果您想傳遞一些數據,我認為您不需要發送者活動。 如果您有很多事情要做,服務是更好的選擇。
您可以在啟動應用時發送BroadCast,就像這樣
Intent intent = new Intent("your broadcast key");
intent.putExtra(COUNTER_VALUE, counter);
sendBroadcast(intent);
因此,無論您在哪里注冊BroadCastReceiver來檢索BroadCast,都可以收到它。 像這樣
public void registerReceiver(Context ctx) {
IntentFilter connFilter = new IntentFilter("your broadcast key");
ctx.registerReceiver(instance, connFilter);
}
public void unregisterReceiver(Context ctx) {
ctx.unregisterReceiver(instance);
instance = null;
}
@Override
public void onReceive(Context context, Intent intent) {
//do things here when you receive your broadcast
}
我有辦法做到這一點
這是一個例子
發件人活動
public class PlayerScreen extends Activity{
static PlayerScreen playerScreen;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
playerScreen = this;
}
public void update(){
.
.
.
}
public static PlayerScreen getInstance() {
return playerScreen;
}
}
LauncherActivity
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(R.layout.main);
try{
PlayerScreen.getInstance().update();
} catch(Exception e){
}finally{
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.