ورود

View Full Version : انجام عملیات به محض وصل شدن گوشی به اینترنت



Amin-rz
چهارشنبه 16 مهر 1393, 14:29 عصر
سلام.
چطور میشه در حالی که برنامه بسته هست به محض اینکه گوشی به نت وصل شد کدی رو اجرا کرد؟
برای دریافت پیامک این کارو کردم ولی برای اینترنت نه.
میدونم که باید از رسیور استفاده کرد.
لطفا با مثال یا لینک یه توضیح بدید.
ممنون.

storm_saeed
چهارشنبه 16 مهر 1393, 14:47 عصر
این دو خط رو به manifest اضافه کنید
<receiver android:name="com.example.MyBroadCastReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>


<service
android:name="com.example.MyService"
>
</service>

اولی broadcast receiver ماست برای اینکه تغییرات wifi و نتورک رو بگیریم دومی هم یک intentservice هست برای کارهایی که قراره امجام بدیم
این کلاس broadcast
public class MyBroadCastReceiver extends BroadcastReceiver {
private static final String TAG = MyBroadCastReceiver.class.getName();

@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "BroadCast Receiver called");
Intent intent1 = new Intent(context, MyService.class);
boolean status = getConncetionStatus();//ye tabe benevisid ke bege interent vasl hast ya na
if(status == true){
context.startService(intent1);
}

}
اینم کلاس سرویس
public class MyService extends IntentService {
public MyService(String name) {
super(name);
}
public MyService() {
super("MyService");
}
private static final String TAG = MyService.class.getName();


@Override
protected void onHandleIntent(Intent arg0) {
Log.d(TAG, "onStartCommand called");
//karhayee ke mikhaid ro inja anjam
}


@Override
public IBinder onBind(Intent intent) {
return null;
}


}

tresa022
جمعه 18 مهر 1393, 18:03 عصر
میشه بیشتر راهنمایی کنید مثلا چطور notificATION نمایش بدم وقتی نت وصل و برنامه بسته بود

tresa022
جمعه 18 مهر 1393, 18:03 عصر
نیاز فوری دارم لطفا راهنماییی کنید

storm_saeed
جمعه 18 مهر 1393, 21:45 عصر
با این کد میتونید نوتیفیکیشن نشون بدید
mNotifyManager = (NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(mContext);
mBuilder.setContentTitle(
r.getString(R.string.test))
.setContentText(
r.getString(R.string.test))
.setSmallIcon(R.drawable.ic_launcher);

کافیه تو کلاس broadcast یا service این کد رو بزنید
mContext از کلاس Context هست

tresa022
جمعه 18 مهر 1393, 22:07 عصر
مرسی
البته هنوز برام کار نکرده
من یه چیزی که متوجه شدم انگار فقط با وصل بودن wifi کار میکنه ؟
درست فهمیدم؟؟؟

Amin-rz
جمعه 18 مهر 1393, 23:09 عصر
چطور ميشه رويداد كليل براي نوتيفيكيشن تعريف كرد؟

gilas1368
شنبه 19 مهر 1393, 09:17 صبح
چطور ميشه رويداد كليل براي نوتيفيكيشن تعريف كرد؟

notification رویداد کلیک نداره شما میتونید با استفاده از متد
setContentIntent() ی PendingIntent رو بهش پاس بدید تا زمانیک روی notification ضربه زده میشه این intent باز بشه و حالا داخل رویداد onCreate میتونید کارهایی رو ک نیاز دارید انجام بدید

tresa022
شنبه 19 مهر 1393, 09:56 صبح
Intent intent = new Intent(getActivity(),MenuFirst.class); intent.putExtra("key", 1);
PendingIntent pIntent = PendingIntent.getActivity(getActivity(), 0, intent, 0);
//
mNotifyManager = (NotificationManager) getActivity().getSystemService(
Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(getActivity());
mBuilder.setContentTitle("updating").setContentText(text).setAutoCancel(true)
.setSmallIcon(R.drawable.biax).setContentIntent(pI ntent);
mNotifyManager.notify(id, mBuilder.build());



این کدا کاملا واضح وگویا هستن اگه مشکلی بود بگید

tresa022
شنبه 19 مهر 1393, 10:57 صبح
این کدای manifest منه

<application android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="com.example.a.MyBroadCastReceiver" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>


<service android:name="com.example.a.MyService">
</service>
اینم کدای این کلاس

public class MyBroadCastReceiver extends BroadcastReceiver {private static final String TAG = MyBroadCastReceiver.class.getName();
private Boolean onlone=false;
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, " اتصال به اینترنت");

Intent in = new Intent(context,MenuFirst.class);
ConnectivityManager cm =
(ConnectivityManager)context.getSystemService(andr oid.content.Context.CONNECTIVITY_SERVICE);

NetworkInfo netInfo = cm.getActiveNetworkInfo();

if (netInfo != null && netInfo.isConnectedOrConnecting())
onlone= true;//ye tabe benevisid ke bege interent vasl hast ya na
if(onlone == true){
context.startService(in);
// Toast.makeText(context, "cc", 100).show();
Log.i("vemk", "اینترنت وصل شد");
}

}


}
اینم کدای سرویس

public class MyService extends IntentService { NotificationManager mNotifyManager;
Builder mBuilder;
public MyService(String name) {
super(name);
}
public MyService() {
super("MyService");
}
private static final String TAG = MyService.class.getName();


@Override
protected void onHandleIntent(Intent arg0) {
Log.i("ve", "دنبا ماله ماس توش نمیمونیم ");
mNotifyManager = (NotificationManager)getSystemService(
Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(getBaseContext());
mBuilder.setContentTitle("some text").setContentText("er").setAutoCancel(true)
.setSmallIcon(R.drawable.biax);
mNotifyManager.notify(1, mBuilder.build());
}


@Override
public IBinder onBind(Intent intent) {
return null;
}


}
نمیدونم چرا کرش میکنه وقتی اتصال به اینتر نت میشم

لطفا راهنمایی کنید

saeed_g21
شنبه 19 مهر 1393, 11:11 صبح
خطای که در Logcat نمایش میده رو بده

tresa022
شنبه 19 مهر 1393, 17:29 عصر
فک کنم چون از جنیموشن و یوویو برای اجرا کردن استفاده میکردم مشکل داشت الان رو گوشی خودم جواب داد