ublic class service_class extends Service { MediaPlayer m; @Override public IBinder onBind(Intent p1) { // TODO: Implement this method return null; } @Override public void onCreate() { //registerReceiver(c,new IntentFilter("A")); m=MediaPlayer.create(this,R.raw.sound); //m.start(); super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { m.start(); // TODO: Implement this method return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { // TODO: Implement this method super.onDestroy(); //m.start(); } }
public class BroadCastRecevier extends BroadcastReceiver { @Override public void onReceive(Context p1, Intent p2) { if(p2.getAction().equals("android.intent.action.USER_PRESENT")){ Intent i=new Intent(p1,service_class.class); p1.startService(i); Toast.makeText(p1,"service",Toast.LENGTH_LONG).show(); } } }
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mycompany.notificathon" > <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" android:resizeableActivity = "true"> <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name=".service_class"/> <receiver android:name=".BroadCastRecevier" android:exported="true"> <intent-filter> <action android:name="android.intent.action.USER_PRESENT"/> </intent-filter> </receiver> </application> </manifest>
registerReceiver()
BroadCastRecevier
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)