I study broadcast messages. In the book D.N. Kolisnichenko - "programming for android" is an example where by pressing the camera button the service is called. But he does not work for me. What is the problem? I thought in the file manifest, so where I just didn’t poke anything there doesn’t work. Or I do not know what exactly the camera button is (either physically or on the display) or I don’t understand what’s wrong. Here is the code from the example:
MainActivity.java //...................................................... import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.content.Intent; import android.content.IntentFilter; public class MainActivity extends AppCompatActivity { MyReceiver iReceiver = new MyReceiver(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); IntentFilter iFilter = new IntentFilter(Intent.ACTION_CAMERA_BUTTON); iFilter.addAction(Intent.ACTION_CAMERA_BUTTON); registerReceiver(iReceiver, iFilter); } @Override protected void onDestroy() { unregisterReceiver(iReceiver); super.onDestroy(); } } //..................................................... MyReceiver.java //.................................................... import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context rcvContext, Intent rcvIntent) { String action = rcvIntent.getAction(); if (action.equals(Intent.ACTION_CAMERA_BUTTON)) { rcvContext.startService(new Intent(rcvContext, MyService.class)); } } } //.......................................................... MyService.java //.......................................................... import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.widget.Toast; public class MyService extends Service { @Override public IBinder onBind(Intent arg0) { return null; } @Override public void onCreate() { super.onCreate(); Toast.makeText(this,"Service started...", Toast.LENGTH_LONG).show(); } @Override public void onDestroy() { super.onDestroy(); Toast.makeText(this, "Service destroyed...", Toast.LENGTH_LONG).show(); } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.ivarious.myapplication"> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> <action android:name="android.intent.action.CAMERA_BUTTON" /> </intent-filter> </activity> <receiver android:name=".MyReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.CAMERA_BUTTON" /> </intent-filter> <service android:name=".MyService"></service> <action android:name="android.intent.action.CAMERA_BUTTON" /> <action android:name="android.intent.extra.KEY_EVENT" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> </receiver> </application> </manifest>
onClick
write herIntent intent = new Intent(ACTION_CAMERA_BUTTON, null); startActivity(intent);
Intent intent = new Intent(ACTION_CAMERA_BUTTON, null); startActivity(intent);
- VAndrJ