2018-04-26 Application name属性

2018-04-26  本文已影响0人  JOJO大魔王

最近做机顶盒开发,需要从开机广播中获取一个“userName”的值放入到Application中。测试过程发现怎么也获取不到,抓了下Log发现自定义的Application没有启动。查了下资料才发现是因为没有设置Application的name属性,导致没有关联自定义的Application。

Receiver代码:

@Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        Gson gson = new Gson();
        Map result = (Map) gson.fromJson(intent.getStringExtra("params"), Map.class);
        editor.putString("userName",(String)result.get("userName"));
        System.out.println("MyReceiver : " + (String)result.get("userName"));
        editor.commit();
    }

Application代码:

sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        userName = sharedPreferences.getString("userName","");
        System.out.println("MyAppliaction onActivityStarted : " + userName);

AndroidManifest.xml:

<application
        android:name=".MyApplication"
        android:allowBackup="true"
...
/>
上一篇下一篇

猜你喜欢

热点阅读