Bonsoir,
Donc je veux passer une valeur dans un broadcast et puis ensuite l'afficher dans un toast mais cela ne fonctionne pas.
Je pense que cela vient de mon manifest...
Merci par avance
Code XML : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.exoent"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <receiver android:name=".MonBroadCastReciever" android:exported="true" android:enabled="true"> <intent-filter> <action android:name="android.intent.action.PHONE_STATE"/> </intent-filter> </receiver> <activity android:name=".ActivityMainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 public class ActivityMainActivity extends AppCompatActivity { int res; private EditText editTextInt; List<Integer> list_int=new ArrayList<Integer>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_activity_main); editTextInt=findViewById(R.id.id_editText); } public void onClickButtonAdd(View view) { Integer val; val = Integer.parseInt(editTextInt.getText().toString()); list_int.add(val); System.out.println(val); } public void onClickCalculer(View view) { for (int i=0; i<list_int.size();i++){ res=res+ list_int.get(i); System.out.println(res); } Intent monIntent; monIntent=new Intent(); Bundle monBundle=new Bundle(); monBundle.putInt("clef_int",res); monIntent.putExtras(monBundle); sendBroadcast(monIntent); } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 public class MonBroadCastReciever extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.i("erreur", " intent recu"); System.out.println("ici"); Bundle bundle =intent.getExtras(); Integer state = bundle.getInt("clef_int"); System.out.println("ici"); Toast.makeText(context, "Action: " + state, Toast.LENGTH_LONG).show(); } }
Partager